sysconfigProvide access to Python’s configuration information提供对Python配置信息的访问

New in version 3.2.版本3.2中新增。

Source code: Lib/sysconfig.py


The sysconfig module provides access to Python’s configuration information like the list of installation paths and the configuration variables relevant for the current platform.sysconfig模块提供对Python配置信息的访问,如安装路径列表和与当前平台相关的配置变量。

Configuration variables配置变量

A Python distribution contains a Makefile and a pyconfig.h header file that are necessary to build both the Python binary itself and third-party C extensions compiled using distutils.Python发行版包含一个Makefile和一个pyconfig.h头文件,这是构建Python二进制文件本身和使用distutils编译的第三方C扩展所必需的。

sysconfig puts all variables found in these files in a dictionary that can be accessed using get_config_vars() or get_config_var().将在这些文件中找到的所有变量放入字典中,可以使用get_config_vars()get_config_var()访问该字典。

Notice that on Windows, it’s a much smaller set.注意,在Windows上,它是一个小得多的集合。

sysconfig.get_config_vars(*args)

With no arguments, return a dictionary of all configuration variables relevant for the current platform.在没有参数的情况下,返回与当前平台相关的所有配置变量的字典。

With arguments, return a list of values that result from looking up each argument in the configuration variable dictionary.对于参数,返回在配置变量字典中查找每个参数所得的值列表。

For each argument, if the value is not found, return None.对于每个参数,如果找不到值,则返回None

sysconfig.get_config_var(name)

Return the value of a single variable name. 返回单个变量name的值。Equivalent to get_config_vars().get(name).等效于get_config_vars().get(name)

If name is not found, return None.如果未找到name,则返回None

Example of usage:用法示例:

>>> 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++']

Installation paths安装路径

Python uses an installation scheme that differs depending on the platform and on the installation options. Python使用的安装方案因平台和安装选项而异。These schemes are stored in sysconfig under unique identifiers based on the value returned by os.name.这些方案基于os.name返回的值存储在sysconfig中的唯一标识符下。

Every new component that is installed using distutils or a Distutils-based system will follow the same scheme to copy its file in the right places.使用distutils或基于distutils的系统安装的每个新组件都将遵循相同的方案将其文件复制到正确的位置。

Python currently supports six schemes:Python目前支持六种方案:

  • posix_prefix: scheme for POSIX platforms like Linux or macOS. This is the default scheme used when Python or a component is installed.:用于POSIX平台(如Linux或macOS)的方案。这是安装Python或组件时使用的默认方案。

  • posix_home: scheme for POSIX platforms used when a home option is used upon installation. :安装时使用home选项时使用的POSIX平台方案。This scheme is used when a component is installed through Distutils with a specific home prefix.当通过具有特定主前缀的Distutils安装组件时,使用此方案。

  • posix_user: scheme for POSIX platforms used when a component is installed through Distutils and the user option is used. :当通过Distutils安装组件并使用user选项时,用于POSIX平台的方案。This scheme defines paths located under the user home directory.此方案定义位于用户主目录下的路径。

  • nt: scheme for NT platforms like Windows.:Windows等NT平台的方案。

  • nt_user: scheme for NT platforms, when the user option is used.:当使用user选项时,NT平台的方案。

  • osx_framework_user: scheme for macOS, when the user option is used.:当使用user选项时,macOS的方案。

Each scheme is itself composed of a series of paths and each path has a unique identifier. 每个方案本身由一系列路径组成,每条路径都有一个唯一的标识符。Python currently uses eight paths:Python目前使用八条路径:

  • stdlib: directory containing the standard Python library files that are not platform-specific.:包含非平台特定的标准Python库文件的目录。

  • platstdlib: directory containing the standard Python library files that are platform-specific.:包含特定于平台的标准Python库文件的目录。

  • platlib: directory for site-specific, platform-specific files.:站点特定、平台特定文件的目录。

  • purelib: directory for site-specific, non-platform-specific files.:站点特定、非平台特定文件的目录。

  • include: directory for non-platform-specific header files.:非平台特定头文件的目录。

  • platinclude: directory for platform-specific header files.:平台特定头文件的目录。

  • scripts: directory for script files.:脚本文件的目录。

  • data: directory for data files.:数据文件的目录。

sysconfig provides some functions to determine these paths.提供了一些函数来确定这些路径。

sysconfig.get_scheme_names()

Return a tuple containing all schemes currently supported in sysconfig.返回包含sysconfig中当前支持的所有方案的元组。

sysconfig.get_default_scheme()

Return the default scheme name for the current platform.返回当前平台的默认方案名称。

Changed in version 3.10:版本3.10中更改: This function was previously named _get_default_scheme() and considered an implementation detail.此函数以前被命名为_get_default_scheme(),并被视为实现细节。

sysconfig.get_preferred_scheme(key)

Return a preferred scheme name for an installation layout specified by key.返回key指定的安装布局的首选方案名称。

key must be either "prefix", "home", or "user".key必须是"prefix""home""user"

The return value is a scheme name listed in get_scheme_names(). 返回值是get_scheme_names()中列出的方案名称。It can be passed to sysconfig functions that take a scheme argument, such as get_paths().它可以传递给采用scheme参数的sysconfig函数,例如get_paths()

New in version 3.10.版本3.10中新增。

sysconfig._get_preferred_schemes()

Return a dict containing preferred scheme names on the current platform. 返回包含当前平台上首选方案名称的dict。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.Python实现者和再发行者可以将他们的首选方案添加到_INSTALL_SCHEMES模块级全局值中,并修改此函数以返回这些方案名称,例如,为系统和语言包管理器提供不同的方案以供使用,因此,其中一方安装的包不会与另一方的包混合。

End users should not use this function, but get_default_scheme() and get_preferred_scheme() instead.最终用户不应使用此函数,而应使用get_default_scheme()get_preferred_scheme())。

New in version 3.10.版本3.10中新增。

sysconfig.get_path_names()

Return a tuple containing all path names currently supported in sysconfig.返回包含sysconfig当前支持的所有路径名的元组。

sysconfig.get_path(name[, scheme[, vars[, expand]]])

Return an installation path corresponding to the path name, from the install scheme named scheme.从名为scheme的安装方案中返回与路径name对应的安装路径。

name has to be a value from the list returned by get_path_names().name必须是get_path_names()返回的列表中的值。

sysconfig stores installation paths corresponding to each path name, for each platform, with variables to be expanded. sysconfig为每个平台存储与每个路径名对应的安装路径,其中包含要扩展的变量。For instance the stdlib path for the nt scheme is: {base}/Lib.例如,nt方案的stdlib路径是:{base}/Lib

get_path() will use the variables returned by get_config_vars() to expand the path. get_path()将使用get_config_vars()返回的变量来扩展路径。All variables have default values for each platform so one may call this function and get the default value.所有变量都有每个平台的默认值,因此可以调用此函数并获取默认值。

If scheme is provided, it must be a value from the list returned by get_scheme_names(). 如果提供了scheme,它必须是get_scheme_names()返回的列表中的值。Otherwise, the default scheme for the current platform is used.否则,将使用当前平台的默认方案。

If vars is provided, it must be a dictionary of variables that will update the dictionary return by get_config_vars().如果提供了vars,则它必须是一个变量字典,将更新get_config_vars()返回的字典。

If expand is set to False, the path will not be expanded using the variables.如果expand设置为False,则不会使用变量展开路径。

If name is not found, raise a KeyError.如果找不到name,则引发KeyError

sysconfig.get_paths([scheme[, vars[, expand]]])

Return a dictionary containing all installation paths corresponding to an installation scheme. 返回包含与安装方案对应的所有安装路径的字典。See get_path() for more information.有关详细信息,请参阅get_path()

If scheme is not provided, will use the default scheme for the current platform.如果未提供scheme,将使用当前平台的默认方案。

If vars is provided, it must be a dictionary of variables that will update the dictionary used to expand the paths.如果提供了vars,它必须是一个变量字典,用于更新用于扩展路径的字典。

If expand is set to false, the paths will not be expanded.如果expand设置为false,则不会展开路径。

If scheme is not an existing scheme, get_paths() will raise a KeyError.如果scheme不是现有方案,get_paths()将引发KeyError

Other functions其他功能

sysconfig.get_python_version()

Return the MAJOR.MINOR Python version number as a string. 以字符串形式返回MAJOR.MINOR Python版本号。Similar to '%d.%d' % sys.version_info[:2].类似于'%d.%d' % sys.version_info[:2]

sysconfig.get_platform()

Return a string that identifies the current platform.返回标识当前平台的字符串。

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.通常包括操作系统名称、版本和体系结构(由‘os.uname()’提供),但所包含的确切信息取决于操作系统;例如,在Linux上,内核版本不是特别重要。

Examples of returned values:返回值示例:

  • linux-i586

  • linux-alpha (?)

  • solaris-2.6-sun4u

Windows will return one of:Windows将返回以下之一:

  • win-amd64 (64bit Windows on AMD64, aka x86_64, Intel64, and EM64T)

  • win32 (all others - specifically, sys.platform is returned)

macOS can return:macOS可以返回:

  • macosx-10.6-ppc

  • macosx-10.4-ppc64

  • macosx-10.3-i386

  • macosx-10.4-fat

For other non-POSIX platforms, currently just returns sys.platform.对于其他非POSIX平台,当前只返回sys.platform

sysconfig.is_python_build()

Return 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.如果正在运行的Python解释器是从源代码构建的,并且是从其构建位置运行的,而不是从运行make install或通过二进制安装程序进行安装的位置运行,则返回True

sysconfig.parse_config_h(fp[, vars])

Parse a config.h-style file.分析config.h样式文件。

fp is a file-like object pointing to the config.h-like file.fp是指向类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.如果将可选字典作为第二个参数传入,则将使用它而不是新字典,并使用文件中读取的值进行更新。

sysconfig.get_config_h_filename()

Return the path of pyconfig.h.返回pyconfig.h的路径。

sysconfig.get_makefile_filename()

Return the path of Makefile.返回Makefile的路径。

Using sysconfig as a script使用sysconfig作为脚本

You can use sysconfig as a script with Python’s -m option:您可以使用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()get_path()get_config_vars()返回的信息。