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


当前回答

from distutils.sysconfig import get_python_lib
print get_python_lib()

其他回答

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

Python 2

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

Python 3

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

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

全局站点包(“dist-packages”)目录列在sys. exe目录下。运行时的路径: Python -m site 要获得更简洁的列表,请在Python代码中的site模块中运行getsitepackages: Python -c '导入站点;打印(site.getsitepackages ()) 注意:在虚拟环境中,旧版本的virtualenv、sys. exe不能使用getsiteppackages。上面的Path会正确地列出virtualenv的site-packages目录。在Python 3中,你可以使用sysconfig模块: Python3 -c 'import sysconfig;打印(sysconfig.get_paths () [" purelib "])” 每个用户站点-packages目录(PEP 370)是Python安装本地包的地方: Python -m site——user-site 如果指向一个不存在的目录,检查Python的退出状态,并查看Python -m site——help了解解释。 提示:运行pip list——user或pip freeze——user会给出每个用户安装的所有站点包的列表。


实用技巧

> <包。__path__让你识别特定包的位置:(详细信息) 导入setuptools as _;打印(_.__path__)” (“/ usr / lib / python2.7 / dist-packages / setuptools”) > <模块。__file__让你识别特定模块的位置:(区别) $ python3 -c "import OS as _;打印(_.__file__)” /usr/lib/python3.6/os.py 执行pip show <package>命令显示debian风格的包信息。 $ PIP show pytest 名称:pytest 版本:3.8.2 概要:pytest:使用Python进行简单而强大的测试 主页:https://docs.pytest.org/en/latest/ 作者:Holger Krekel, Bruno Oliveira, Ronny Pfannschmidt, Floris Bruynooghe, Brianna laughher, Florian Bruhin等 Author-email:没有 许可证:MIT许可证 地点:/home/peter/.local/lib/python3.4/site-packages 需要:更多itertools, atomicwrites, setuptools, attrs, pathlib2, six, py, plug - ggy

在基于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 distutils.sysconfig import get_python_lib
print get_python_lib()

对于Ubuntu,

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

...不正确。

它会指向/usr/lib/pythonX.X / dist-packages

此文件夹仅包含操作系统为运行程序自动安装的包。

在ubuntu上,包含通过setup_tools\easy_install\pip安装的软件包的site-packages文件夹将位于/usr/local/lib/pythonX.X / dist-packages

如果用例与安装或阅读源代码有关,那么第二个文件夹可能更有用。

如果您不使用Ubuntu,那么将第一个代码框复制粘贴到终端中可能是安全的。