python的site-packages目录是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31384639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:53:54  来源:igfitidea点击:

What is python's site-packages directory?

python

提问by Omer Dagan

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

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

采纳答案by Omer Dagan

site-packagesis the target directoryof 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-packagesby default.

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

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
  • Unix(纯)1prefix/lib/pythonX.Y/site-packages
  • Unix(非纯): exec-prefix/lib/pythonX.Y/site-packages
  • 视窗: prefix\Lib\site-packages

1Puremeans that the module uses only Python code. Non-purecan contain C/C++ code as well.

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

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

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



Useful reading

有用的阅读

回答by Anthony Perot

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 alwayspart 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'

回答by Shital Shah

When you use --useroption 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.

当您将--useroption 与 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 --useroption with pip, the package gets installed in global folder given by:

当您不将--useroption 与 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。