我如何找到我的站点包目录的位置?


当前回答

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

(或者只是site.getsitepackages()[0]的第一项)

其他回答

附注:建议的解决方案(distutils.sysconfig.get_python_lib())在存在多个site-packages目录(本文推荐)时不起作用。它将只返回主site-packages目录。

唉,我也没有更好的解决办法。Python似乎不跟踪站点包目录,只跟踪其中的包。

所有的答案(或者:重复同样的答案)都是不充分的。你要做的是:

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

最后一行显示了安装目录。适用于Ubuntu,而上面的不能。不要问我关于windows或其他目录的问题,但由于它是easy_install默认使用的完全相同的目录,所以它可能在easy_install工作的任何地方都是正确的(所以,任何地方,甚至mac)。玩得开心。注意:原始代码中有很多脏话。

PIP展会将提供有关包装的所有细节: https://pip.pypa.io/en/stable/reference/pip_show/ [pip show][1]

获取位置:

pip show <package_name>| grep Location

在Linux中,你可以通过以下方式进入site-packages文件夹:

cd $(python -c "import site; print(site.getsitepackages()[0])")

之前没有提到的一些东西,我认为是有用的,如果你安装了两个版本的Python,比如3.8和3.5,在你的机器上可能有两个名为site-packages的文件夹。在这种情况下,您可以使用以下命令指定python版本:

py -3.5 -c "import site; print(site.getsitepackages()[1])

前面提到的get_python_lib函数的另一个注意事项是:在某些平台上,不同的目录用于特定于平台的模块(例如:需要编译的模块)。如果将plat_specific=True传递给函数,则可以获得平台特定包的站点包。