如何找到我的 Python site-packages 目录的位置?

如何找到我的 site-packages 目录的位置?

答案

>>> import site; site.getsitepackages()
['/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']

网站包目录有两种类型, 全局目录和每个用户目录。

  1. 运行时,全局站点软件包(“ dist-packages ”)目录在sys.path中列出:

    python -m site

    要获得更简洁的列表,请使用 Python 代码从site 模块运行getsitepackages

    python -c "import site; print(site.getsitepackages())"

    注意:如果 virtualenvs getsitepackages 不可用 ,则上面的sys.path会正确列出 virtualenv 的 site-packages 目录。

  2. 每个用户的站点软件包目录( PEP 370 )是 Python 安装本地软件包的位置:

    python -m site --user-site

    如果这指向一个不存在的目录,请检查 Python 的退出状态,并参阅python -m site --help进行解释。

    提示:运行pip list --userpip 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
python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"
from distutils.sysconfig import get_python_lib
print(get_python_lib())
python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"
python -m site --user-site
>>> import django
>>> dir(django)
['VERSION', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 'get_version']
>>> print django.__path__
['/Library/Python/2.6/site-packages/django']
import distutils.sysconfig
print distutils.sysconfig.get_python_lib()
import sys, os
print os.sep.join([sys.prefix, 'lib', 'python' + sys.version[:3], 'site-packages'])
from distutils.sysconfig import get_python_lib
print get_python_lib()
>>> import site; site.getsitepackages()
['/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
>>> import site; site.getsitepackages()
['/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/site-python', '/Library/Python/2.7/site-packages']
from setuptools.command.easy_install import easy_install
class easy_install_default(easy_install):
  """ class easy_install had problems with the fist parameter not being
      an instance of Distribution, even though it was. This is due to
      some import-related mess.
      """

  def __init__(self):
    from distutils.dist import Distribution
    dist = Distribution()
    self.distribution = dist
    self.initialize_options()
    self._dry_run = None
    self.verbose = dist.verbose
    self.force = None
    self.help = 0
    self.finalized = 0

e = easy_install_default()
import distutils.errors
try:
  e.finalize_options()
except distutils.errors.DistutilsError:
  pass

print e.install_dir

旁注:如果存在多个 site-packages 目录(如本文建议 ),则建议的解决方案( distutils.sysconfig.get_python_lib() )不起作用。它只会返回主 site-packages 目录。

,我也没有更好的解决方案。 Python 似乎不跟踪站点软件包目录,而只是跟踪其中的软件包。