ubuntu 如何在Docker镜像/容器中的虚拟环境下安装Python包?

vmdwslir  于 12个月前  发布在  Docker
关注(0)|答案(2)|浏览(131)

docker文件(下面的示例)使用ubuntu作为基础镜像,试图安装其他python包,抛出警告/错误,基于警告,我可以创建一个虚拟环境并安装所有需要的包。
要使用这个镜像/容器,我如何确保虚拟环境被激活,这样当脚本/程序需要运行时,所有的python包都可用
还是有其他方法可以做到这一点。
Dockerfile

FROM ubuntu:23.10 

RUN apt-get update; apt-get -y install python3.11 python3-pip

RUN pip install pandas

警告/错误

error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
    python3-xyz, where xyz is the package you are trying to
    install.

    If you wish to install a non-Debian-packaged Python package,
    create a virtual environment using python3 -m venv path/to/venv.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
    sure you have python3-full installed.

    If you wish to install a non-Debian packaged Python application,
    it may be easiest to use pipx install xyz, which will manage a
    virtual environment for you. Make sure you have pipx installed.

    See /usr/share/doc/python3.11/README.venv for more information.

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.
ijnw1ujt

ijnw1ujt1#

不管出于什么原因,容器希望您通过apt包管理器而不是pip(python)安装pandas
它告诉你,如果你想通过pip安装,你应该使用virtualenv等等。它告诉你基本上你需要做什么,以便通过pip使用virtualenv安装。

rfbsl7qr

rfbsl7qr2#

一个可能的Dockerfile看起来像这样:

FROM ubuntu:23.10

WORKDIR /app

RUN apt-get update; apt-get -y install python3.11 python3-pip python3.11-venv
RUN python3.11 -m venv .venv && /app/.venv/bin/python -m pip install pandas

这将在/app/.venv中创建虚拟环境并在其中安装pandas。
要运行使用pandas模块的脚本,请使用/app/.venv/bin/python命令。

相关问题