问题:python的site-packages目录是什么?

site-packages在与Python相关的各种文章中都提到了该目录。它是什么?如何使用它?

The directory site-packages is mentioned in various Python related articles. What is it? How to use it?


回答 0

site-packages是手动构建的Python软件包的目标目录。从源代码构建和安装Python软件包时(使用distutils,可能通过执行python setup.py install),site-packages默认情况下会找到已安装的模块。

有标准位置:

  • Unix(纯)1prefix/lib/pythonX.Y/site-packages
  • Unix(非纯): exec-prefix/lib/pythonX.Y/site-packages
  • 视窗: prefix\Lib\site-packages

1 Pure表示该模块仅使用Python代码。非纯也可以包含C / C ++代码。

site-packages是默认情况下是Python 搜索路径的一部分,因此之后可以轻松导入在那里安装的模块。


有用的阅读

site-packages is the target directory of manually built Python packages. When you build and install Python packages from source (using distutils, probably by executing python setup.py install), you will find the installed modules in site-packages by default.

There are standard locations:

  • Unix (pure)1: prefix/lib/pythonX.Y/site-packages
  • Unix (non-pure): exec-prefix/lib/pythonX.Y/site-packages
  • Windows: prefix\Lib\site-packages

1Pure means that the module uses only Python code. Non-pure can contain C/C++ code as well.

site-packages is by default part of the Python search path, so modules installed there can be imported easily afterwards.


Useful reading


回答 1

当您将--useroption与pip一起使用时,该软件包将安装在用户的文件夹中,而不是全局文件夹中,并且您无需以管理员权限运行pip命令。

用户的packages文件夹的位置可以使用以下方法找到:

python -m site --user-site

这将打印如下内容:

C:\Users\%USERNAME%\AppData\Roaming\Python\Python35\site-packages

当您不对--userpip 使用option时,该软件包将安装在以下给定的全局文件夹中:

python -c "import site; print(site.getsitepackages())"

这将打印如下内容:

['C:\\Program Files\\Anaconda3', 'C:\\Program Files\\Anaconda3\\lib\\site-packages'

注意:以上打印值适用于在Windows 10上安装了默认值的Anaconda 4.x的情况。

When you use --user option with pip, the package gets installed in user’s folder instead of global folder and you won’t need to run pip command with admin privileges.

The location of user’s packages folder can be found using:

python -m site --user-site

This will print something like:

C:\Users\%USERNAME%\AppData\Roaming\Python\Python35\site-packages

When you don’t use --user option with pip, the package gets installed in global folder given by:

python -c "import site; print(site.getsitepackages())"

This will print something like:

['C:\\Program Files\\Anaconda3', 'C:\\Program Files\\Anaconda3\\lib\\site-packages'

Note: Above printed values are for On Windows 10 with Anaconda 4.x installed with defaults.


回答 2

site-packages只是Python安装其模块的位置。

无需“查找”,python知道自己在哪里可以找到它,该位置始终是PYTHONPATH(sys.path)的一部分。

您可以通过编程方式找到它:

import sys
site_packages = next(p for p in sys.path if 'site-packages' in p)
print site_packages

‘/Users/foo/.envs/env1/lib/python2.7/site-packages’

site-packages is just the location where Python installs its modules.

No need to “find it”, python knows where to find it by itself, this location is always part of the PYTHONPATH (sys.path).

Programmatically you can find it this way:

import sys
site_packages = next(p for p in sys.path if 'site-packages' in p)
print site_packages

‘/Users/foo/.envs/env1/lib/python2.7/site-packages’


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。