sysconfig
— 提供对 Python 配置信息的访问
¶
3.2 版新增。
源代码: Lib/sysconfig.py
sysconfig
模块提供对 Python 配置信息的访问,像安装路径列表和当前平台相关配置变量。
Python 分发包含
Makefile
和
pyconfig.h
头文件是必要的,以构建 Python 二进制文件本身和编译第三方 C 扩展使用
distutils
.
sysconfig
将在这些文件中找到的所有变量放在字典中,可以访问使用
get_config_vars()
or
get_config_var()
.
注意,在 Windows,其集要小得多。
sysconfig.
get_config_vars
(
*args
)
¶
不带自变量,返回当前平台相关的所有配置变量字典。
带自变量,返回在配置变量字典中找到每个自变量结果的值列表。
对于每个自变量,若找不到值,返回
None
.
sysconfig.
get_config_var
(
name
)
¶
返回值为单变量
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
.
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.
Python 目前支持 7 种方案:
Each scheme is itself composed of a series of paths and each path has a unique identifier. Python currently uses eight paths:
sysconfig
provides some functions to determine these paths.
sysconfig.
get_scheme_names
(
)
¶
Return a tuple containing all schemes currently supported in
sysconfig
.
sysconfig.
get_path_names
(
)
¶
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.
若
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 return by
get_config_vars()
.
若
expand
被设为
False
, the path will not be expanded using the variables.
若
name
找不到,返回
None
.
sysconfig.
get_paths
(
[
scheme
[
,
vars
[
,
expand
]
]
]
)
¶
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
is not an existing scheme,
get_paths()
will raise a
KeyError
.
sysconfig.
get_python_version
(
)
¶
返回
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. for IRIX the architecture isn’t particularly important (IRIX only runs on SGI hardware), but for Linux the kernel version isn’t particularly important.
返回值的范例:
Windows will return one of:
Mac OS X 会返回:
For other non-POSIX platforms, currently just returns
sys.platform
.
sysconfig.
is_python_build
(
)
¶
返回
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
安装
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
(
)
¶
Return the path of
pyconfig.h
.
sysconfig.
get_makefile_filename
(
)
¶
Return the path of
Makefile
.
sysconfig
作为脚本
¶
可以使用
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"
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()
.