site
— 特定站点配置挂钩
¶
源代码: Lib/site.py
此模块在初始化期间自动导入。
可以抑制自动导入使用解释器的
-S
选项。
Importing this module normally appends site-specific paths to the module search path and adds
callables
,包括
help()
to the built-in namespace. However, Python startup option
-S
blocks this and this module can be safely imported with no automatic modifications to the module search path or additions to the builtins. To explicitly trigger the usual site-specific additions, call the
main()
函数。
3.3 版改变:
Importing the module used to trigger paths manipulation even when using
-S
.
It starts by constructing up to four directories from a head and a tail part. For the head part, it uses
sys.prefix
and
sys.exec_prefix
; empty heads are skipped. For the tail part, it uses the empty string and then
lib/site-packages
(在 Windows) 或
lib/pythonX.Y[t]/site-packages
(on Unix and macOS). (The optional suffix “t” indicates the
free threading
build, and is appended if
"t"
is present in the
sys.abiflags
constant.) For each of the distinct head-tail combinations, it sees if it refers to an existing directory, and if so, adds it to
sys.path
and also inspects the newly added path for configuration files.
3.5 版改变: 已移除对 site-python 目录的支持。
Changed in version 3.13:
在 Unix,
Free threading
Python installations are identified by the “t” suffix in the version-specific directory name, such as
lib/python3.13t/
.
If a file named “pyvenv.cfg” exists one directory above sys.executable, sys.prefix and sys.exec_prefix are set to that directory and it is also checked for site-packages (sys.base_prefix and sys.base_exec_prefix will always be the “real” prefixes of the Python installation). If “pyvenv.cfg” (a bootstrap configuration file) contains the key “include-system-site-packages” set to anything other than “true” (case-insensitive), the system-level prefixes will not be searched for site-packages; otherwise they will.
A path configuration file is a file whose name has the form
name.pth
and exists in one of the four directories mentioned above; its contents are additional items (one per line) to be added to
sys.path
. Non-existing items are never added to
sys.path
, and no check is made that the item refers to a directory rather than a file. No item is added to
sys.path
more than once. Blank lines and lines beginning with
#
are skipped. Lines starting with
import
(followed by space or tab) are executed.
注意
An executable line in a
.pth
file is run at every Python startup, regardless of whether a particular module is actually going to be used. Its impact should thus be kept to a minimum. The primary intended purpose of executable lines is to make the corresponding module(s) importable (load 3rd-party import hooks, adjust
PATH
etc). Any other initialization is supposed to be done upon a module’s actual import, if and when it happens. Limiting a code chunk to a single line is a deliberate measure to discourage putting anything more complex here.
Changed in version 3.13:
The
.pth
files are now decoded by UTF-8 at first and then by the
区域设置编码
if it fails.
例如,假设
sys.prefix
and
sys.exec_prefix
被设为
/usr/local
。那么 Python X.Y 库安装在
/usr/local/lib/pythonX.Y
。假设这拥有子目录
/usr/local/lib/pythonX.Y/site-packages
带有 3 个子目录,
foo
,
bar
and
spam
,和 2 个路径配置文件,
foo.pth
and
bar.pth
。假定
foo.pth
包含下列:
# foo package configuration
foo
bar
bletch
and
bar.pth
包含:
# bar package configuration
bar