gpt4 book ai didi

python - 即使在dockerfile/requirements.txt中添加了numpy之后,在docker容器中使用numpy运行python代码的“Module not found error”

转载 作者:行者123 更新时间:2023-12-02 19:51:36 26 4
gpt4 key购买 nike

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

我想让它与numpy一起使用

在我尝试使其与numpy一起使用之前,evrything可以正常工作。

我在python代码中写了这一行:import numpy as np
这些是我在容器中安装numpy的步骤:

我在dockerfile中为numpy添加了pip安装:
pip install numpy==1.14.3(带有和不带有版本...)
我收到此错误:

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

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

这是我的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"

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

关于python - 即使在dockerfile/requirements.txt中添加了numpy之后,在docker容器中使用numpy运行python代码的“Module not found error”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60283366/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com