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


当前回答

这应该适用于虚拟环境内外的所有发行版,因为它是“低技术”性质。os模块总是位于'site-packages'的父目录中

import os; print(os.path.dirname(os.__file__) + '/site-packages')

要将dir更改为site-packages dir,我使用以下别名(在*nix系统上):

alias cdsp='cd $(python -c "import os; print(os.path.dirname(os.__file__))"); cd site-packages'

其他回答

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

您应该尝试这个命令来确定pip的安装位置

Python 2

pip show six | grep "Location:" | cut -d " " -f2

Python 3

pip3 show six | grep "Location:" | cut -d " " -f2

我做了一个非常简单的函数来完成这项工作

import site


def get_site_packages_dir():
        return [p for p  in site.getsitepackages()
                if p.endswith(("site-packages", "dist-packages"))][0]

get_site_packages_dir()
# '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages'

如果你想使用终端检索结果:

python3 -c "import site;print([p for p in site.getsitepackages() if p.endswith(('site-packages', 'dist-packages')) ][0])"

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages


这应该适用于虚拟环境内外的所有发行版,因为它是“低技术”性质。os模块总是位于'site-packages'的父目录中

import os; print(os.path.dirname(os.__file__) + '/site-packages')

要将dir更改为site-packages dir,我使用以下别名(在*nix系统上):

alias cdsp='cd $(python -c "import os; print(os.path.dirname(os.__file__))"); cd 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)。玩得开心。注意:原始代码中有很多脏话。