How do I find the location of my Python site-packages directory?
如何找到我的站点包目录的位置?
1 2 | >>> import site; site.getsitepackages() ['/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages'] |
第一个项目与
From"How to install Django"documentation(though this is useful to more than just Django anslation)-execute the following from the shell:
1 | python -c"from distutils.sysconfig import get_python_lib; print(get_python_lib())" |
Formatted for readability(rather than use as a one-liner),that looks like the following:
ZZU1
有两种类型的站点包目录,全局和每个用户。
当您运行时,全局站点包("dist packages")目录列在
1 | python -m site |
要获得更简洁的列表,请使用python代码从站点模块运行
1 | python -c"import site; print(site.getsitepackages())" |
注意:由于virtualenvs getsitepackages不可用,上面的
每个用户站点包目录(PEP 370)是Python安装本地包的位置:
1 | python -m site --user-site |
如果指向一个不存在的目录,请检查python的退出状态,并参阅
提示:运行
实用技巧
允许您标识特定包的位置:(详细信息).__path__ 1
2$ python -c"import setuptools as _; print(_.__path__)"
['/usr/lib/python2.7/dist-packages/setuptools'] 允许您识别特定模块的位置:(差异).__file__ 1
2$ python3 -c"import os as _; print(_.__file__)"
/usr/lib/python3.6/os.py运行
pip show 显示debian样式的包信息:1
2
3
4
5
6
7
8
9
10$ pip show pytest
Name: pytest
Version: 3.8.2
Summary: pytest: simple powerful testing with Python
Home-page: https://docs.pytest.org/en/latest/
Author: Holger Krekel, Bruno Oliveira, Ronny Pfannschmidt, Floris Bruynooghe, Brianna Laugher, Florian Bruhin and others
Author-email: None
License: MIT license
Location: /home/peter/.local/lib/python3.4/site-packages
Requires: more-itertools, atomicwrites, setuptools, attrs, pathlib2, six, py, pluggy
For Ubuntu,
1 | python -c"from distutils.sysconfig import get_python_lib; print get_python_lib()" |
这是不正确的。
这是你要去的地方
此折叠仅包含您操作系统的包,自动安装程序以运行。
在UBUNTU上,存放通过设置工具安装的包装的现场包会在EDOCX1&9〕
如果使用的案例与安装或阅读源代码有关,则第二个曲线很可能更为有用。
如果你不使用Ubuntu,你很可能是安全的拷贝-将第一个代码盒放入终端。
This is what worked for me:
1 | python -m site --user-site |
让我们说你安装了Django包Import it and type in dir(Django).它将展示你,所有的功能和属性。Python解释器中的类型
1 2 3 4 5 | >>> import django >>> dir(django) ['VERSION', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 'get_version'] >>> print django.__path__ ['/Library/Python/2.6/site-packages/django'] |
如果你安装了商标,你也可以做同样的事情。
这是给雪豹的。但我认为这应该在一般情况下进行。
As others have noted,
1 2 | import distutils.sysconfig print distutils.sysconfig.get_python_lib() |
"考虑到
1 2 | import sys, os print os.sep.join([sys.prefix, 'lib', 'python' + sys.version[:3], 'site-packages']) |
(It also adds
上下文是什么?你不应直接与你的
The native system packages installed with python instalment in Debian based systems can be found at:
/usr/lib/python2.7/dist-packages/
OSX-
By using this small code:
1 2 | from distutils.sysconfig import get_python_lib print get_python_lib() |
然而,通过
BLCK1/
或者一个人可以简单地写下下面的命令,列举Python包装的所有路径。
1 2 | >>> import site; site.getsitepackages() ['/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages'] |
注释:定位可能基于你的骨头,如OSX。
1 2 | >>> 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'] |
所有的答案(或:相同的答案重复过和过)都是不适当的。你想干什么?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 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 |
最后一条线路告诉你安装指令。Works on Ubuntu,whereas the above ones don't.it's probably correct everywhere easy install uses by default(so,everywhere,even macs).玩得开心注:原始代码在这一标题中有许多词语。
a side-note:the proposed solution(EDOCX1&7)does not work when there is more than one site-packages directory(as recommended by this article).它只会返回主站点包装目录。
阿拉斯,我没有更好的解决办法。Pyton doesn't seem to keep track of site-packages directories,just the packages within them.
这是我的作品。它将使你两个远程包和现场包。如果狐狸不在Python之路无论如何你都很好。
1 2 | import sys; print [f for f in sys.path if f.endswith('packages')] |
输出(UBUNTU安装):
1 2 3 | ['/home/username/.local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages'] |
This should work on all distributions in and out of virtual environmental because it's"low-tech"nature.骨模块在站点包装的父系目录中的永久残留
1 | import os; print(os.path.dirname(os.__file__) + '/site-packages') |
To change dir to the site-packages dir I use the following alias(on*nix systems):
1 | alias cdsp='cd $(python -c"import os; print(os.path.dirname(os.__file__))"); cd site-packages' |
函数表示已经完成:在一些平台上使用了不同的方向模块(EG:需要编译的模块)。如果你通过了EDOCX1&6,到功能上,你得到了一个站点包,用于特殊的平台包。
1 2 | from distutils.sysconfig import get_python_lib print get_python_lib() |
PIP Show将提供有关包的所有详细信息:https://pip.pypa.io/en/stable/reference/pip_show/[pip show][1]
要获取位置:
1 | pip show <package_name>| grep Location |
现代的stdlib方法是使用
- stdlib: directory containing the standard Python library files that are not platform-specific.
- platstdlib: directory containing the standard Python library files that are platform-specific.
- platlib: directory for site-specific, platform-specific files.
- purelib: directory for site-specific, non-platform-specific files.
- include: directory for non-platform-specific header files.
- platinclude: directory for platform-specific header files.
- scripts: directory for script files.
- data: directory for data files.
在大多数情况下,发现这个问题的用户会对"purelib"路径感兴趣(在某些情况下,您也可能对"platlib"感兴趣)。与当前接受的答案不同,根据是否激活了virtualenv,此方法不应出现错误行为。
在系统级(这是Mac OS上的python 3.7.0):
1 2 3 | >>> import sysconfig >>> sysconfig.get_paths()['purelib'] '/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages' |
有了Venv,你就会得到这样的东西
1 2 3 | >>> import sysconfig >>> sysconfig.get_paths()['purelib'] '/private/tmp/.venv/lib/python3.7/site-packages' |
shell脚本也可用于显示这些详细信息,您可以通过将
1 | python -m sysconfig |
注:
回答旧问题。但是用ipython来做这个。
1 2 3 4 | pip install ipython ipython import imaplib imaplib? |
这将给出以下关于imaplib包的输出-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | Type: module String form: <module 'imaplib' from '/usr/lib/python2.7/imaplib.py'> File: /usr/lib/python2.7/imaplib.py Docstring: IMAP4 client. Based on RFC 2060. Public class: IMAP4 Public variable: Debug Public functions: Internaldate2tuple Int2AP ParseFlags Time2Internaldate |
对于我正在进行的一个项目,我必须做一些稍微不同的事情:找到相对于基本安装前缀的相对站点包目录。如果站点包文件夹在
与作弊者的回答类似,我的解决方案深入到distutils的内部,以找到在
1 2 3 4 5 6 7 8 9 10 | import sys import os from distutils.command.install import INSTALL_SCHEMES if os.name == 'nt': scheme_key = 'nt' else: scheme_key = 'unix_prefix' print(INSTALL_SCHEMES[scheme_key]['purelib'].replace('$py_version_short', (str.split(sys.version))[0][0:3]).replace('$base', '')) |
这应该打印类似于
如果它已经添加到
1 2 3 | import sys print(' '.join(sys.path)) |