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


当前回答

对于那些正在使用诗歌的人,你可以通过诗歌调试找到你的虚拟环境路径:

$ poetry debug

Poetry
Version: 1.1.4
Python:  3.8.2

Virtualenv
Python:         3.8.2
Implementation: CPython
Path:           /Users/cglacet/.pyenv/versions/3.8.2/envs/my-virtualenv
Valid:          True

System
Platform: darwin
OS:       posix
Python:   /Users/cglacet/.pyenv/versions/3.8.2

使用这些信息,您可以列出站点包:

ls /Users/cglacet/.pyenv/versions/3.8.2/envs/my-virtualenv/lib/python3.8/site-packages/

其他回答

解决方案:

在virtualenv之外-提供全局站点包的路径, 在virtualenv内部-提供virtualenv的站点包

...这是一行代码:

python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"

格式化为可读性(而不是作为一行程序使用),看起来如下所示:

from distutils.sysconfig import get_python_lib
print(get_python_lib())

来源:一个非常旧的“如何安装Django”文档(尽管这不仅仅对安装Django有用)

from distutils.sysconfig import get_python_lib
print get_python_lib()

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

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)。玩得开心。注意:原始代码中有很多脏话。

正如其他人所注意到的,distutils。Sysconfig有相关的设置:

import distutils.sysconfig
print distutils.sysconfig.get_python_lib()

...尽管默认的site.py做了一些更粗糙的事情,如下所述:

import sys, os
print os.sep.join([sys.prefix, 'lib', 'python' + sys.version[:3], 'site-packages'])

(它还添加了${sys. exe。Prefix}/lib/site-python,为sys. python添加两个路径。Exec_prefix也是,如果常量不同的话)。

也就是说,背景是什么?你不应该直接破坏你的站点包;Setuptools /distutils将用于安装,并且您的程序可能运行在一个virtualenv中,其中您的pythonpath完全是用户本地的,因此它也不应该假设直接使用系统站点包。

对于那些正在使用诗歌的人,你可以通过诗歌调试找到你的虚拟环境路径:

$ poetry debug

Poetry
Version: 1.1.4
Python:  3.8.2

Virtualenv
Python:         3.8.2
Implementation: CPython
Path:           /Users/cglacet/.pyenv/versions/3.8.2/envs/my-virtualenv
Valid:          True

System
Platform: darwin
OS:       posix
Python:   /Users/cglacet/.pyenv/versions/3.8.2

使用这些信息,您可以列出站点包:

ls /Users/cglacet/.pyenv/versions/3.8.2/envs/my-virtualenv/lib/python3.8/site-packages/