sysconfig
posix_user
nt_user
osx_framework_user
posix_home
posix_prefix
nt
sys.monitoring — 执行事件监视
sys.monitoring
Added in version 3.2.
源代码: Lib/sysconfig.py
The sysconfig 模块提供对 Python 配置信息的访问,像安装路径列表和当前平台相关配置变量。
Python 分发包含 Makefile 和 pyconfig.h 头文件是必要的,以构建 Python 二进制文件本身和编译第三方 C 扩展使用 setuptools .
Makefile
pyconfig.h
setuptools
sysconfig 将在这些文件中找到的所有变量放在字典中,可以访问使用 get_config_vars() or get_config_var() .
get_config_vars()
get_config_var()
注意,在 Windows,其集要小得多。
不带自变量,返回当前平台相关的所有配置变量字典。
带自变量,返回在配置变量字典中找到每个自变量结果的值列表。
对于每个自变量,若找不到值,返回 None .
None
返回值为单变量 name 。相当于 get_config_vars().get(name) .
get_config_vars().get(name)
若 name 找不到,返回 None .
用法范例:
>>> import sysconfig >>> sysconfig.get_config_var('Py_ENABLE_SHARED') 0 >>> sysconfig.get_config_var('LIBDIR') '/usr/local/lib' >>> sysconfig.get_config_vars('AR', 'CXX') ['ar', 'g++']
Python uses an installation scheme that differs depending on the platform and on the installation options. These schemes are stored in sysconfig under unique identifiers based on the value returned by os.name . The schemes are used by package installers to determine where to copy files to.
os.name
Python currently supports nine schemes:
posix_prefix : scheme for POSIX platforms like Linux or macOS. This is the default scheme used when Python or a component is installed.
posix_home : scheme for POSIX platforms, when the home option is used. This scheme defines paths located under a specific home prefix.
posix_user : scheme for POSIX platforms, when the user option is used. This scheme defines paths located under the user’s home directory ( site.USER_BASE ).
site.USER_BASE
posix_venv : scheme for Python virtual environments on POSIX platforms; by default it is the same as posix_prefix .
Python virtual environments
nt : scheme for Windows. This is the default scheme used when Python or a component is installed.
nt_user : scheme for Windows, when the user option is used.
nt_venv : scheme for Python virtual environments on Windows; by default it is the same as nt .
venv : a scheme with values from either posix_venv or nt_venv depending on the platform Python runs on.
osx_framework_user : scheme for macOS, when the user option is used.
Each scheme is itself composed of a series of paths and each path has a unique identifier. Python currently uses eight paths:
stdlib : directory containing the standard Python library files that are not platform-specific.
platstdlib : directory containing the standard Python library files that are platform-specific.
platlib : directory for site-specific, platform-specific files.
purelib : directory for site-specific, non-platform-specific files (‘pure’ Python).
包括 : directory for non-platform-specific header files for the Python C-API.
platinclude : directory for platform-specific header files for the Python C-API.
脚本 :脚本文件的目录。
data :数据文件的目录。
This scheme is designed to be the most convenient solution for users that don’t have write permission to the global site-packages directory or don’t want to install into it.
Files will be installed into subdirectories of site.USER_BASE (written as userbase hereafter). This scheme installs pure Python modules and extension modules in the same location (also known as site.USER_SITE ).
userbase
site.USER_SITE
路径
安装目录
stdlib
userbase/lib/pythonX.Y
platstdlib
platlib
userbase/lib/pythonX.Y/site-packages
purelib
包括
userbase/include/pythonX.Y
脚本
userbase/bin
data
userbase\PythonXY
userbase\PythonXY\site-packages
userbase\PythonXY\Include
userbase\PythonXY\Scripts
userbase/lib/python
userbase/lib/python/site-packages
The idea behind the “home scheme” is that you build and maintain a personal stash of Python modules. This scheme’s name is derived from the idea of a “home” directory on Unix, since it’s not unusual for a Unix user to make their home directory have a layout similar to /usr/ or /usr/local/ . This scheme can be used by anyone, regardless of the operating system they are installing for.
/usr/
/usr/local/
home/lib/python
home/include/python
platinclude
home/bin
home
The “prefix scheme” is useful when you wish to use one Python installation to perform the build/install (i.e., to run the setup script), but install modules into the third-party module directory of a different Python installation (or something that looks like a different Python installation). If this sounds a trifle unusual, it is—that’s why the user and home schemes come before. However, there are at least two known cases where the prefix scheme will be useful.
First, consider that many Linux distributions put Python in /usr , rather than the more traditional /usr/local . This is entirely appropriate, since in those cases Python is part of “the system” rather than a local add-on. However, if you are installing Python modules from source, you probably want them to go in /usr/local/lib/python2.X 而不是 /usr/lib/python2.X .
/usr
/usr/local
/usr/local/lib/python2.X
/usr/lib/python2.X
Another possibility is a network filesystem where the name used to write to a remote directory is different from the name used to read it: for example, the Python interpreter accessed as /usr/local/bin/python might search for modules in /usr/local/lib/python2.X , but those modules would have to be installed to, say, /mnt/@server/export/lib/python2.X .
/usr/local/bin/python
/mnt/@server/export/lib/python2.X
prefix/lib/pythonX.Y
prefix/lib/pythonX.Y/site-packages
prefix/include/pythonX.Y
prefix/bin
prefix
prefix\Lib
prefix\Lib\site-packages
prefix\Include
prefix\Scripts
sysconfig provides some functions to determine these installation paths.
Return a tuple containing all schemes currently supported in sysconfig .
Return the default scheme name for the current platform.
Added in version 3.10: This function was previously named _get_default_scheme() and considered an implementation detail.
_get_default_scheme()
3.11 版改变: When Python runs from a virtual environment, the venv scheme is returned.
Return a preferred scheme name for an installation layout specified by key .
key must be either "prefix" , "home" ,或 "user" .
"prefix"
"home"
"user"
The return value is a scheme name listed in get_scheme_names() . It can be passed to sysconfig functions that take a scheme argument, such as get_paths() .
get_scheme_names()
get_paths()
Added in version 3.10.
3.11 版改变: When Python runs from a virtual environment and key="prefix" , venv scheme is returned.
key="prefix"
Return a dict containing preferred scheme names on the current platform. Python implementers and redistributors may add their preferred schemes to the _INSTALL_SCHEMES module-level global value, and modify this function to return those scheme names, to e.g. provide different schemes for system and language package managers to use, so packages installed by either do not mix with those by the other.
_INSTALL_SCHEMES
End users should not use this function, but get_default_scheme() and get_preferred_scheme() 代替。
get_default_scheme()
get_preferred_scheme()
Return a tuple containing all path names currently supported in sysconfig .
Return an installation path corresponding to the path name , from the install scheme named scheme .
name has to be a value from the list returned by get_path_names() .
get_path_names()
sysconfig stores installation paths corresponding to each path name, for each platform, with variables to be expanded. For instance the stdlib path for the nt scheme is: {base}/Lib .
{base}/Lib
get_path() will use the variables returned by get_config_vars() to expand the path. All variables have default values for each platform so one may call this function and get the default value.
get_path()
若 scheme is provided, it must be a value from the list returned by get_scheme_names() . Otherwise, the default scheme for the current platform is used.
若 vars is provided, it must be a dictionary of variables that will update the dictionary returned by get_config_vars() .
若 expand 被设为 False , the path will not be expanded using the variables.
False
若 name 找不到,引发 KeyError .
KeyError
Return a dictionary containing all installation paths corresponding to an installation scheme. See get_path() 了解更多信息。
若 scheme is not provided, will use the default scheme for the current platform.
若 vars is provided, it must be a dictionary of variables that will update the dictionary used to expand the paths.
若 expand is set to false, the paths will not be expanded.
若 scheme 不是现有方案, get_paths() 将引发 KeyError .
返回 MAJOR.MINOR Python version number as a string. Similar to '%d.%d' % sys.version_info[:2] .
MAJOR.MINOR
'%d.%d' % sys.version_info[:2]
返回标识当前平台的字符串。
This is used mainly to distinguish platform-specific build directories and platform-specific built distributions. Typically includes the OS name and version and the architecture (as supplied by ‘os.uname()’), although the exact information included depends on the OS; e.g., on Linux, the kernel version isn’t particularly important.
返回值的范例:
linux-i586
linux-alpha (?)
solaris-2.6-sun4u
Windows will return one of:
win-amd64 (64bit Windows on AMD64, aka x86_64, Intel64, and EM64T)
win32 (all others - specifically, sys.platform is returned)
macOS can return:
macosx-10.6-ppc
macosx-10.4-ppc64
macosx-10.3-i386
macosx-10.4-fat
对于其它非 POSIX 平台,目前仅仅返回 sys.platform .
sys.platform
返回 True if the running Python interpreter was built from source and is being run from its built location, and not from a location resulting from e.g. running make install or installing via a binary installer.
True
make install
剖析 config.h 样式文件。
config.h
fp is a file-like object pointing to the config.h 像文件。
A dictionary containing name/value pairs is returned. If an optional dictionary is passed in as the second argument, it is used instead of a new dictionary, and updated with the values read in the file.
返回路径为 pyconfig.h .
返回路径为 Makefile .
可以使用 sysconfig 作为脚本采用 Python 的 -m 选项:
$ python -m sysconfig Platform: "macosx-10.4-i386" Python version: "3.2" Current installation scheme: "posix_prefix" Paths: data = "/usr/local" include = "/Users/tarek/Dev/svn.python.org/py3k/Include" platinclude = "." platlib = "/usr/local/lib/python3.2/site-packages" platstdlib = "/usr/local/lib/python3.2" purelib = "/usr/local/lib/python3.2/site-packages" scripts = "/usr/local/bin" stdlib = "/usr/local/lib/python3.2" Variables: AC_APPLE_UNIVERSAL_BUILD = "0" AIX_GENUINE_CPLUSPLUS = "0" AR = "ar" ARFLAGS = "rc" ...
This call will print in the standard output the information returned by get_platform() , get_python_version() , get_path() and get_config_vars() .
get_platform()
get_python_version()
builtins — 内置对象
builtins
键入搜索术语或模块、类、函数名称。