简体   繁体   English

python的站点包目录是什么?

[英]What is python's site-packages directory?

The directory site-packages is mentioned in various Python related articles.目录site-packages在各种 Python 相关文章中都有提及。 What is it?它是什么? How to use it?如何使用它?

site-packages is the target directory of manually built Python packages. site-packages是手动构建的 Python 包的目标目录 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.当您从源代码构建和安装 Python 包时(使用distutils ,可能通过执行python setup.py install ),默认情况下您会在site-packages找到已安装的模块。

There are standard locations:有标准位置:

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

1 Pure means that the module uses only Python code. 1 Pure表示该模块仅使用 Python 代码。 Non-pure can contain C/C++ code as well.非纯也可以包含 C/C++ 代码。

site-packages is by default part of the Python search path , so modules installed there can be imported easily afterwards.默认情况下, site-packages是 Python搜索路径的一部分,因此安装在那里的模块之后可以轻松导入。


Useful reading有用的阅读

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.当您将--user选项与 pip 一起使用时,软件包将安装在用户的文件夹而不是全局文件夹中,您无需以管理员权限运行 pip 命令。

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:当您不将--user选项与 pip 一起使用时,该软件包将安装在以下给出的全局文件夹中:

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.注意:以上打印值适用于安装了默认值的 Anaconda 4.x 的 Windows 10。

site-packages is just the location where Python installs its modules. site-packages 只是 Python 安装其模块的位置。

No need to "find it", python knows where to find it by itself, this location is always part of the PYTHONPATH (sys.path).无需“查找”,python 自己知道在哪里可以找到它,该位置始终是 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' '/Users/foo/.envs/env1/lib/python2.7/site-packages'

在我的 CentOS7.9 Linux(一个 RedHat 克隆)上,它位于~/.local/lib/python3.9/site-packages/ ,不需要将它包含在 PYTHONPATH 变量中。

According to here :根据这里

A Python installation has a site-packages directory inside the module directory. Python 安装在模块目录中有一个 site-packages 目录。 This directory is where user installed packages are dropped.此目录是删除用户安装的软件包的位置。

Though it doesn't explain why the word site is chosen , it explains what this directory is meant for .虽然它没有解释为什么选择站点这个词,但它解释了这个目录的含义

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM