sysconfig —— 提供对 Python 配置信息的访问支持

3.2 版新加入.


sysconfig 模块提供了对 Python 配置信息的访问支持,比如安装路径列表和有关当前平台的配置变量。


Python 的发行版中包含一个 Makefile 和一个 pyconfig.h 头文件,这是构建 Python 二进制文件本身和用 distutils 编译的第三方 C 语言扩展所必需的。

sysconfig 将这些文件中的所有变量放在一个字典对象中,可用 get_config_vars()get_config_var() 访问。

请注意在 Windows 上,这是一个小得多的集合。




对于每个参数,如果未找到值,则返回 None


返回单个变量 name 的值。 等价于 get_config_vars().get(name)

如果未找到 name,则返回 None


>>> import sysconfig
>>> sysconfig.get_config_var('Py_ENABLE_SHARED')
>>> sysconfig.get_config_var('LIBDIR')
>>> sysconfig.get_config_vars('AR', 'CXX')
['ar', 'g++']


Python 会使用根据平台和安装选项区别处理的安装方案。 这些方案保存在 sysconfig 中基于 的返回值来确定的唯一标识符下。

每个使用 distutils 或基于 Distutils 的系统安装的新组件都将遵循同样的方案将其文件拷贝到正确的位置。

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_home: scheme for POSIX platforms used when a home option is used upon installation. This scheme is used when a component is installed through Distutils with a specific home prefix.

  • posix_user: scheme for POSIX platforms used when a component is installed through Distutils and the user option is used. This scheme defines paths located under the user home directory.

  • nt: scheme for NT platforms like Windows.

  • nt_user: scheme for NT platforms, when the user option is used.

  • 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.

  • 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.


Return a tuple containing all schemes currently supported in sysconfig.


Return the default scheme name for the current platform.

3.10 版更變: This function was previously named _get_default_scheme() and considered an implementation detail.


Return a preferred scheme name for an installation layout specified by key.

key must be either "prefix", "home", or "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().

3.10 版新加入.


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.

End users should not use this function, but get_default_scheme() and get_preferred_scheme() instead.

3.10 版新加入.


Return a tuple containing all path names currently supported in sysconfig.

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

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().

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.

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.

If 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.

If vars is provided, it must be a dictionary of variables that will update the dictionary return by get_config_vars().

If expand is set to False, the path will not be expanded using the variables.

If name is not found, raise a 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.

If scheme is not provided, will use the default scheme for the current platform.

If vars is provided, it must be a dictionary of variables that will update the dictionary used to expand the paths.

If expand is set to false, the paths will not be expanded.

If scheme is not an existing scheme, get_paths() will raise a KeyError.



Return the MAJOR.MINOR Python version number as a string. Similar to '%d.%d' % sys.version_info[:2].


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.


  • linux-i586

  • linux-alpha (?)

  • solaris-2.6-sun4u


  • win-amd64 (在 AMD64, aka x86_64, Intel64, 和 EM64T上的64位 Windows )

  • 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


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.

sysconfig.parse_config_h(fp[, vars])

Parse a config.h-style file.

fp is a file-like object pointing to the config.h-like file.

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 的目录

Using sysconfig as a script

You can use sysconfig as a script with Python's -m option:

$ python -m sysconfig
Platform: "macosx-10.4-i386"
Python version: "3.2"
Current installation scheme: "posix_prefix"

        data = "/usr/local"
        include = "/Users/tarek/Dev/"
        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"

        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().