🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
### 导航 - [索引](../genindex.xhtml "总目录") - [模块](../py-modindex.xhtml "Python 模块索引") | - [下一页](builtins.xhtml "builtins --- 内建对象") | - [上一页](sys.xhtml "sys --- 系统相关的参数和函数") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) » - zh\_CN 3.7.3 [文档](../index.xhtml) » - [Python 标准库](index.xhtml) » - [Python运行时服务](python.xhtml) » - $('.inline-search').show(0); | # [`sysconfig`](#module-sysconfig "sysconfig: Python's configuration information") --- Provide access to Python's configuration information 3\.2 新版功能. **源代码:** [Lib/sysconfig.py](https://github.com/python/cpython/tree/3.7/Lib/sysconfig.py) \[https://github.com/python/cpython/tree/3.7/Lib/sysconfig.py\] - - - - - - The [`sysconfig`](#module-sysconfig "sysconfig: Python's configuration information") module provides access to Python's configuration information like the list of installation paths and the configuration variables relevant for the current platform. ## 配置变量 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`](distutils.xhtml#module-distutils "distutils: Support for building and installing Python modules into an existing Python installation."). [`sysconfig`](#module-sysconfig "sysconfig: Python's configuration information") puts all variables found in these files in a dictionary that can be accessed using [`get_config_vars()`](#sysconfig.get_config_vars "sysconfig.get_config_vars") or [`get_config_var()`](#sysconfig.get_config_var "sysconfig.get_config_var"). Notice that on Windows, it's a much smaller set. `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`. `sysconfig.``get_config_var`(*name*)Return the value of a single variable *name*. Equivalent to `get_config_vars().get(name)`. If *name* is not found, return `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`](#module-sysconfig "sysconfig: Python's configuration information") under unique identifiers based on the value returned by [`os.name`](os.xhtml#os.name "os.name"). Every new component that is installed using [`distutils`](distutils.xhtml#module-distutils "distutils: Support for building and installing Python modules into an existing Python installation.") or a Distutils-based system will follow the same scheme to copy its file in the right places. Python currently supports seven schemes: - *posix\_prefix*: scheme for Posix platforms like Linux or Mac OS X. 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. 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`](#module-sysconfig "sysconfig: Python's configuration information") provides some functions to determine these paths. `sysconfig.``get_scheme_names`()Return a tuple containing all schemes currently supported in [`sysconfig`](#module-sysconfig "sysconfig: Python's configuration information"). `sysconfig.``get_path_names`()Return a tuple containing all path names currently supported in [`sysconfig`](#module-sysconfig "sysconfig: Python's configuration information"). `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.get_path_names "sysconfig.get_path_names"). [`sysconfig`](#module-sysconfig "sysconfig: Python's configuration information") 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()`](#sysconfig.get_path "sysconfig.get_path") will use the variables returned by [`get_config_vars()`](#sysconfig.get_config_vars "sysconfig.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()`](#sysconfig.get_scheme_names "sysconfig.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()`](#sysconfig.get_config_vars "sysconfig.get_config_vars"). If *expand* is set to `False`, the path will not be expanded using the variables. If *name* is not found, return `None`. `sysconfig.``get_paths`(\[*scheme*\[, *vars*\[, *expand*\]\]\])Return a dictionary containing all installation paths corresponding to an installation scheme. See [`get_path()`](#sysconfig.get_path "sysconfig.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()`](#sysconfig.get_paths "sysconfig.get_paths") will raise a [`KeyError`](exceptions.xhtml#KeyError "KeyError"). ## 其他功能 `sysconfig.``get_python_version`()Return the `MAJOR.MINOR` Python version number as a string. Similar to `'%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. 返回值的示例: - linux-i586 - linux-alpha (?) - solaris-2.6-sun4u Windows将返回以下之一: - win-amd64 (在 AMD64, aka x86\_64, Intel64, 和 EM64T上的64位 Windows ) - win32 (all others - specifically, sys.platform is returned) Mac OS X 返回: - macosx-10.6-ppc - macosx-10.4-ppc64 - macosx-10.3-i386 - macosx-10.4-fat 对于其他非-POSIX 平台, 目前只是返回 [`sys.platform`](sys.xhtml#sys.platform "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. `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. `sysconfig.``get_config_h_filename`()返回 `pyconfig.h` 的目录 `sysconfig.``get_makefile_filename`()返回 `Makefile` 的目录 ## Using [`sysconfig`](#module-sysconfig "sysconfig: Python's configuration information") as a script You can use [`sysconfig`](#module-sysconfig "sysconfig: Python's configuration information") 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" 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()`](#sysconfig.get_platform "sysconfig.get_platform"), [`get_python_version()`](#sysconfig.get_python_version "sysconfig.get_python_version"), [`get_path()`](#sysconfig.get_path "sysconfig.get_path") and [`get_config_vars()`](#sysconfig.get_config_vars "sysconfig.get_config_vars"). ### 导航 - [索引](../genindex.xhtml "总目录") - [模块](../py-modindex.xhtml "Python 模块索引") | - [下一页](builtins.xhtml "builtins --- 内建对象") | - [上一页](sys.xhtml "sys --- 系统相关的参数和函数") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) » - zh\_CN 3.7.3 [文档](../index.xhtml) » - [Python 标准库](index.xhtml) » - [Python运行时服务](python.xhtml) » - $('.inline-search').show(0); | © [版权所有](../copyright.xhtml) 2001-2019, Python Software Foundation. Python 软件基金会是一个非盈利组织。 [请捐助。](https://www.python.org/psf/donations/) 最后更新于 5月 21, 2019. [发现了问题](../bugs.xhtml)? 使用[Sphinx](http://sphinx.pocoo.org/)1.8.4 创建。