gpt4 book ai didi

python - apk python软件包未安装在Alpine Docker Image中

转载 作者:行者123 更新时间:2023-12-02 21:04:54 25 4
gpt4 key购买 nike

我有一个dockerfile如下:

FROM python:3.7.5-alpine3.10

RUN apk update

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

RUN apk add --no-cache cython3

CMD [ "sh", "ls"]

当我用 docker run -it --rm mycontainer /bin/sh进入容器时,cython似乎未安装。我想念什么?
/usr/src/app # which python
/usr/local/bin/python
/usr/src/app # python
Python 3.7.5 (default, Oct 21 2019, 20:13:45)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cython
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cython'

最佳答案

Alpine使用此路径/usr/lib/python3.7/site-packages安装了python pacakges,只需在容器内运行命令,您将看到已安装软件包。您需要将此路径添加到python搜索路径中。

RUN apk add --no-cache cython3
ENV PYTHONPATH /usr/lib/python3.7/site-packages

PYTHONPATH

Augment the default search path for module files. The format is the same as the shell’s PATH: one or more directory pathnames separated by os.pathsep (e.g. colons on Unix or semicolons on Windows). Non-existent directories are silently ignored.

In addition to normal directories, individual PYTHONPATH entries may refer to zipfiles containing pure Python modules (in either source or compiled form). Extension modules cannot be imported from zipfiles.

The default search path is installation dependent, but generally begins with prefix/lib/pythonversion (see PYTHONHOME above). It is always appended to PYTHONPATH.

An additional directory will be inserted in the search path in front of PYTHONPATH as described above under Interface options. The search path can be manipulated from within a Python program as the variable sys.path.



python envvar PYTHONPATH

更新:

要进行pip安装,您需要使用 -m

When called with -m module-name, the given module is located on the Python module path and executed as a script.



python3-cmdline

你可以测试
RUN apk add --no-cache cython3
ENV PYTHONPATH /usr/lib/python3.7/site-packages
RUN python -m pip install requests
RUN python -m pip list
#import test
RUN python -c "import requests"

关于python - apk python软件包未安装在Alpine Docker Image中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58703800/

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