简体   繁体   English

站点包目录未添加到 sys.path?

[英]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 to sys.path . [添加目录] 最方便的方法是将路径配置文件添加到 Python 路径上已经存在的目录中......路径配置文件的扩展名为.pth ,并且每一行必须包含一个将附加的路径到sys.path

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 virtualenv,sys.path和site-packages - virtualenv, sys.path and site-packages 在 Python 3 虚拟环境中,我在 site-packages 目录中添加了一个 mypaths.pth 文件,但 pth 文件中的目录未添加到 sys.path - In a Python 3 virtual environment I have added a mypaths.pth file within the site-packages dir but the dirs in the pth file are not added to sys.path 为什么 /usr/lib/python3.10/site-packages 不在 sys.path 中? - Why /usr/lib/python3.10/site-packages not in sys.path? 正确将site-packages文件夹添加到Sublime Text 3 sys.path - Correct add site-packages folder to Sublime Text 3 sys.path Python 可嵌入 Zip 文件在 sys.path 中不包含 lib/site-packages - Python Embeddable Zip File Doesn't Include lib/site-packages in sys.path Virtualenv,no-site-packages,sys.path - Virtualenv, no-site-packages, sys.path 如何将“ C:\\ Users \\ Myname \\ Anaconda3 \\ envs \\ pytorch \\ lib \\ site-packages”之类的路径永久添加到sys.path? - How to add a path like “C:\Users\Myname\Anaconda3\envs\pytorch\lib\site-packages” to sys.path permanently? 如何在保留用户站点包的同时避免在 Python 的 sys.path 中有当前目录? - How to avoid having current directory in Python's sys.path while keeping user site packages? 防止脚本目录被添加到 Python 中的 sys.path 3 - Prevent script dir from being added to sys.path in Python 3 dist-packages和sys.path - dist-packages and sys.path
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM