"Module not found error"在 docker 容器中使用 numpy 运行 python 代码,即使在 dockerfile /requirements 中添加 numpy 后也



我正在一个开发容器中运行一些非常基本的python代码

我想让它与numpy 一起工作

在我尝试让它与numpy一起工作之前,每一个都非常完美。

我在我的python代码中写了这样一行:import numpy as np

以下是我在容器中安装numpy所遵循的步骤:

我在dockerfile中为numpy添加了pip安装:pip install numpy==1.14.3(带和不带版本…(我得到了这个错误:

import numpy as np
ModuleNotFoundError: No module named 'numpy'

我尝试在requirements.txt中添加numpy,并在dockerfile 中添加COPY requirements.txt /tmp/pip-tmp/

这是我的Dockerfile:

FROM python:3
# Avoid warnings by switching to noninteractive
ENV DEBIAN_FRONTEND=noninteractive
# This Dockerfile adds a non-root user with sudo access. Use the "remoteUser"
# property in devcontainer.json to use it. On Linux, the container user's GID/UIDs
# will be updated to match your local UID/GID (when using the dockerFile property).
# See https://aka.ms/vscode-remote/containers/non-root-user for details.
ARG USERNAME=vscode
ARG USER_UID=1000
ARG USER_GID=$USER_UID
# Uncomment the following COPY line and the corresponding lines in the `RUN` command if you wish to
# include your requirements in the image itself. It is suggested that you only do this if your
# requirements rarely (if ever) change.
COPY requirements.txt /tmp/pip-tmp/
# Configure apt and install packages
RUN apt-get update 
&& pip install numpy==1.14.3 
&& apt-get -y install --no-install-recommends apt-utils dialog 2>&1 
#
# Verify git, process tools, lsb-release (common in install instructions for CLIs) installed
&& apt-get -y install git openssh-client iproute2 procps lsb-release 
#
# Install pylint
&& apt-get -y install libc-dev 
&& apt-get -y install build-essential 
&& pip install -U pip 
&& pip --disable-pip-version-check --no-cache-dir install pylint 
#&& pip install --no-cache-dir numpy scipy pandas matplotlib 
#
# Update Python environment based on requirements.txt
&& pip --disable-pip-version-check --no-cache-dir install -r /tmp/pip-tmp/requirements.txt 
&& rm -rf /tmp/pip-tmp 
#
# Create a non-root user to use if preferred - see https://aka.ms/vscode-remote/containers/non-root-user.
&& groupadd --gid $USER_GID $USERNAME 
&& useradd -s /bin/bash --uid $USER_UID --gid $USER_GID -m $USERNAME 
# [Optional] Add sudo support for the non-root user
&& apt-get install -y sudo 
&& echo $USERNAME ALL=(root) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME
&& chmod 0440 /etc/sudoers.d/$USERNAME 
#
# Clean up
&& apt-get autoremove -y 
&& apt-get clean -y 
&& rm -rf /var/lib/apt/lists/*
# Switch back to dialog for any ad-hoc use of apt-get
ENV DEBIAN_FRONTEND=dialog

我得到了相同的错误

(我构建了我的容器,并在每个步骤后重新运行它(

如果你知道如何帮助我修复它,请让我知道

要确保您没有使用不同的python环境,请通过安装python3-virtualenv在容器中创建一个python虚拟环境。并使用以下命令激活它。

RUN python3 -m virtualenv --python=/usr/bin/python3 /opt/venv
ENV PATH="/opt/venv/bin:$PATH"

如果问题仍然存在,请尝试手动删除容器映像并重新创建它。

一种理论是:您无意中安装了Debian打包的Python。因此,现在您有两个Python,一个来自Docker映像(您正在pip安装(,另一个在命令行上运行,这是不同的。

最新更新