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


当前回答

在基于Debian的系统中,使用python安装的本机系统包可以在以下位置找到:

/usr/lib/python2.7/dist-packages /

在OSX - /Library/Python/2.7/site-packages中

通过使用这个小代码:

from distutils.sysconfig import get_python_lib
print get_python_lib()

然而,通过pip安装的软件包列表可以在以下位置找到:

/ usr / local / bin /

或者可以简单地编写以下命令来列出python包所在的所有路径。

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

注意:位置可能会根据你的操作系统而变化,比如在OSX中

>>> 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']

其他回答

在基于Debian的系统中,使用python安装的本机系统包可以在以下位置找到:

/usr/lib/python2.7/dist-packages /

在OSX - /Library/Python/2.7/site-packages中

通过使用这个小代码:

from distutils.sysconfig import get_python_lib
print get_python_lib()

然而,通过pip安装的软件包列表可以在以下位置找到:

/ usr / local / bin /

或者可以简单地编写以下命令来列出python包所在的所有路径。

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

注意:位置可能会根据你的操作系统而变化,比如在OSX中

>>> 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

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

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

$ 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/

这对我来说很管用:

python -m site --user-site

解决方案:

在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有用)