如何查找我的 Python 站点包目录的位置?
2022-09-05 01:01:47
如何查找我的站点包目录的位置?
有两种类型的站点包目录:全局目录和每用户目录。
运行全局站点包(“dist 包”)目录时,将在 中列出:sys.path
python -m site
有关在 Python 代码中从站点模块运行的更简洁的列表:getsitepackages
python -c 'import site; print(site.getsitepackages())'
谨慎:在虚拟环境中,getsitepackages不适用于旧版本的virtualenv
,但是,从上面将正确列出virtualenv的站点包目录。在 Python 3 中,您可以改用 sysconfig 模块:sys.path
python3 -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])'
每用户站点包目录 (PEP 370) 是 Python 安装本地包的位置:
python -m site --user-site
如果这指向不存在的目录,请检查Python的退出状态,并查看说明。python -m site --help
提示:正在运行 或 为你提供每个用户安装的所有站点包的列表。pip list --user
pip freeze --user
<package>.__path__
允许您识别特定软件包的位置:(详细信息)
$ python -c "import setuptools as _; print(_.__path__)"
['/usr/lib/python2.7/dist-packages/setuptools']
<module>.__file__
允许您识别特定模块的位置:(差异)
$ python3 -c "import os as _; print(_.__file__)"
/usr/lib/python3.6/os.py
运行以显示 Debian 风格的软件包信息:pip show <package>
$ pip show pytest
Name: pytest
Version: 3.8.2
Summary: pytest: simple powerful testing with Python
Home-page: https://docs.pytest.org/en/latest/
Author: Holger Krekel, Bruno Oliveira, Ronny Pfannschmidt, Floris Bruynooghe, Brianna Laugher, Florian Bruhin and others
Author-email: None
License: MIT license
Location: /home/peter/.local/lib/python3.4/site-packages
Requires: more-itertools, atomicwrites, setuptools, attrs, pathlib2, six, py, pluggy
>>> import site; site.getsitepackages()
['/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
(或仅包含第一项site.getsitepackages()[0]
)