[英]site-packages directory not being added to sys.path?
My question is why /usr/lib/python3.8/site-packages/
is not being added to sys.path
;我的问题是为什么/usr/lib/python3.8/site-packages/
没有被添加到sys.path
; I expect it to have been added by the site module.我希望它是由站点模块添加的。
Some details: from within a Docker container, I am using pip install -e
to install some Python packages in editable mode.一些细节:在 Docker 容器中,我使用pip install -e
在可编辑模式下安装一些 Python 包。 The packages get installed to /usr/lib/python3.8/site-packages
;软件包安装到/usr/lib/python3.8/site-packages
; however this directory is not in sys.path
so I cannot import them.但是此目录不在sys.path
中,因此我无法导入它们。
The site module docs say:站点模块文档说:
lib/pythonX.Y/site-packages
... if it refers to an existing directory, and if so, adds it to sys.path. lib/pythonX.Y/site-packages
... 如果它引用一个现有目录,如果是,则将其添加到 sys.path。 I've confirmed that this directory exists so I expect it to be added.我已经确认这个目录存在,所以我希望它被添加。
python3 -m site
prints: python3 -m site
打印:
sys.path = [
'/',
'/usr/lib/python38.zip',
'/usr/lib/python3.8',
'/usr/lib/python3.8/lib-dynload',
'/usr/local/lib/python3.8/dist-packages',
'/usr/lib/python3/dist-packages',
]
USER_BASE: '/root/.local' (doesn't exist)
USER_SITE: '/root/.local/lib/python3.8/site-packages' (doesn't exist)
ENABLE_USER_SITE: True
thanks for any help.谢谢你的帮助。
I had the same problem for a python-3.8 installation (3.9 was fine), I added:我在安装 python-3.8 时遇到了同样的问题(3.9 很好),我补充说:
COPY usercustomize.py /root/
to the Dockerfile
, with the contents of usercustomize.py
:到Dockerfile
,其中包含usercustomize.py
的内容:
import sys
if sys.version_info[:2] == (3, 8):
sys.path.append("/usr/lib/python3.8/site-packages")
This doesn't answer why , but adding this line to the Dockerfile seems to work around the issue:这不能回答为什么,但是将此行添加到 Dockerfile 似乎可以解决此问题:
RUN echo "/usr/lib/python3.x/site-packages" >> /usr/local/lib/python3.x/dist-packages/site-packages.pth
where x
is your Python minor version.其中x
是您的 Python 次要版本。
This workaround is based on the Modifying Python's Search Path section of the docs which says:此解决方法基于文档的修改 Python 的搜索路径部分,该部分说:
The most convenient way [to add a directory] is to add a path configuration file to a directory that's already on Python's path... Path configuration files have an extension of
.pth
, and each line must contain a single path that will be appended tosys.path
. [添加目录] 最方便的方法是将路径配置文件添加到 Python 路径上已经存在的目录中......路径配置文件的扩展名为.pth
,并且每一行必须包含一个将附加的路径到sys.path
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.