>>> import site; site.getsitepackages()
['/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages'] (或者只是第一个带有site.getsitepackages()[0] )
从“如何安装 Django” 文档 (虽然这不仅仅是 Django 安装有用) - 从 shell 执行以下命令:
python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"格式化为可读性(而不是用作单行),如下所示:
from distutils.sysconfig import get_python_lib
print(get_python_lib())
有两种类型的站点包目录, 全局和每用户 。
运行时, sys.path中列出了全局站点包(“ dist-packages ”)目录:
python -m site要获得更简洁的列表,请在 Python 代码中从站点模块运行getsitepackages :
python -c "import site; print(site.getsitepackages())" 注意:使用 virtualenvs getitepackages 不可用 ,但上面的sys.path将正确列出 virtualenv 的 site-packages 目录。
每个用户的站点包目录( 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运行pip show <package>以显示 Debian 风格的包信息:
$ 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