我该如何找到我的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)都可能是正确的。祝玩得愉快。注意:原始代码中有很多脏话。
这个对我来说有效。 它将获取dist-packages和site-packages文件夹。 如果该文件夹不在Python路径上,那么它也无济于事。
import sys;
print [f for f in sys.path if f.endswith('packages')]
输出(Ubuntu 安装):
['/home/username/.local/lib/python2.7/site-packages',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages']
由于其“低技术”性质,这应该适用于所有发行版,无论是在虚拟环境内还是外部。os模块始终驻留在“site-packages”的父目录中。
import os; print(os.path.dirname(os.__file__) + '/site-packages')
为了将目录更改为site-packages目录,我在*nix系统上使用以下别名:
alias cdsp='cd $(python -c "import os; print(os.path.dirname(os.__file__))"); cd site-packages'
\
)作为路径分隔符。只是在 Windows 上使用反斜杠更为常见。 - Pyramid Newbiedistutils.sysconfig.get_python_lib()
)在存在多个site-packages目录时无法正常工作(如本文建议)。它只会返回主要的site-packages目录。get_python_lib()
时会显示用于创建虚拟环境的 Python 的 site-packages 而不是虚拟环境的 site-packages。 - Piotr Dobrogost关于get_python_lib
函数的补充说明: 在一些平台上,会为特定于平台的模块(例如需要编译的模块)使用不同的目录。如果您将plat_specific=True
传递给该函数,则可以获取特定于平台的包的站点包。
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
回答一个老问题。但是这个需要使用IPython。
pip install ipython
ipython
import imaplib
imaplib?
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
对于那些使用poetry的人,你可以通过poetry debug
指令找到虚拟环境路径:
$ 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/
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
dist-packages
,现在已经包括了 dist-packages
)。 - Angel
pip show <软件包名称>
。 - joshuakcockrell