sysconfig – Interpreter Compile-time Configuration

Purpose:Access the configuration settings used to build Python.
Available In:2.7 and later

In Python 2.7 sysconfig has been extracted from distutils to become a stand-alone module. It includes functions for determining the settings used to compile and install the current interpreter.

Configuration Variables

You can access the build-time configuration settings through two functions. get_config_vars() returns a dictionary mapping the configuration variable names to values.

import sysconfig

config_values = sysconfig.get_config_vars()
print 'Found %d configuration settings' % len(config_values.keys())
print

print 'Some highlights:'

print
print '  Installation prefixes:'
print '    prefix={prefix}'.format(**config_values)
print '    exec_prefix={exec_prefix}'.format(**config_values)

print
print '  Version info:'
print '    py_version={py_version}'.format(**config_values)
print '    py_version_short={py_version_short}'.format(**config_values)
print '    py_version_nodot={py_version_nodot}'.format(**config_values)

print
print '  Base directories:'
print '    base={base}'.format(**config_values)
print '    platbase={platbase}'.format(**config_values)
print '    userbase={userbase}'.format(**config_values)
print '    srcdir={srcdir}'.format(**config_values)

print
print '  Compiler and linker flags:'
print '    LDFLAGS={LDFLAGS}'.format(**config_values)
print '    BASECFLAGS={BASECFLAGS}'.format(**config_values)
print '    Py_ENABLE_SHARED={Py_ENABLE_SHARED}'.format(**config_values)

The level of detail available through the sysconfig API depends on the platform where your program is running. On POSIX systems such as Linux and OS X, the Makefile used to build the interpreter and config.h header file generated for the build are parsed and all of the variables found within are available. On non-POSIX-compliant systems such as Windows, the settings are limited to a few paths, filename extensions, and version details.

$ python sysconfig_get_config_vars.py

Found 517 configuration settings

Some highlights:

  Installation prefixes:
    prefix=/Library/Frameworks/Python.framework/Versions/2.7
    exec_prefix=/Library/Frameworks/Python.framework/Versions/2.7

  Version info:
    py_version=2.7.2
    py_version_short=2.7
    py_version_nodot=27

  Base directories:
    base=/Users/dhellmann/Envs/pymotw
    platbase=/Users/dhellmann/Envs/pymotw
    userbase=/Users/dhellmann/Library/Python/2.7
    srcdir=/Users/sysadmin/build/v2.7.2

  Compiler and linker flags:
    LDFLAGS=-arch i386 -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.6.sdk -isysroot /Developer/SDKs/MacOSX10.6.sdk -g
    BASECFLAGS=-fno-strict-aliasing -fno-common -dynamic
    Py_ENABLE_SHARED=0

If you pass variable names to get_config_vars(), the return value is a list created by appending all of the values for those variables together.

import sysconfig

bases = sysconfig.get_config_vars('base', 'platbase', 'userbase')
print 'Base directories:'
for b in bases:
    print '  ', b

This example builds a list of all of the installation base directories where modules can be found on the current system.

$ python sysconfig_get_config_vars_by_name.py

Base directories:
   /Users/dhellmann/Envs/pymotw
   /Users/dhellmann/Envs/pymotw
   /Users/dhellmann/Library/Python/2.7

When you only need a single configuration value, use get_config_var() to retrieve it.

import sysconfig

print 'User base directory:', sysconfig.get_config_var('userbase')
print 'Unknown variable   :', sysconfig.get_config_var('NoSuchVariable')

If the variable is not found, get_config_var() returns None instead of raising an exception.

$ python sysconfig_get_config_var.py

User base directory: /Users/dhellmann/Library/Python/2.7
Unknown variable   : None

Installation Paths

sysconfig is primarily meant to be used by installation and packaging tools. As a result, while it provides access to general configuration settings such as the interpreter version, it is primarily focused on the information needed to locate parts of the Python distribution currently installed on a system. The locations used by for installing a package depend on the scheme used.

A scheme is a set of platform-specific default directories organized based on the platform’s packaging standards and guidelines. There are different schemes for installing into a site-wide location or a private directory owned by the user. The full set of schemes can be accessed with get_scheme_names().

import sysconfig

for name in sysconfig.get_scheme_names():
    print name

There is no concept of a “current scheme” per se. The default scheme depends on the platform, and the actual scheme used depends on options given to the installation program. If the current system is running a POSIX-compliant operating system, the default is posix_prefix. Otherwise the default is os.name.

$ python sysconfig_get_scheme_names.py

nt
nt_user
os2
os2_home
osx_framework_user
posix_home
posix_prefix
posix_user

Each scheme defines a set of paths used for installing packages. For a list of the path names, use get_path_names().

import sysconfig

for name in sysconfig.get_path_names():
    print name

Some of the paths may be the same for a given scheme, but installers should not make any assumptions about what the actual paths are. Each name has a particular semantic meaning, so the correct name should be used to find the path for a given file during installation.

Name Description
stdlib Standard Python library files, not platform-specific
platstdlib Standard Python library files, platform-specific
platlib Site-specific, platform-specific files
purelib Site-specific, non-platform-specific files
include Header files, not platform-specific
platinclude Header files, platform-specific
scripts Executable script files
data Data files
$ python sysconfig_get_path_names.py

stdlib
platstdlib
purelib
platlib
include
scripts
data

Use get_paths() to retrieve the actual directories associated with a scheme.

import sysconfig
import pprint

for scheme in ['posix_prefix', 'posix_user']:
    print scheme
    print '=' * len(scheme)
    pprint.pprint(sysconfig.get_paths(scheme=scheme))
    print 

This example shows the difference between the system-wide paths uses for posix_prefix and the user-specific values for posix_user.

$ python sysconfig_get_paths.py

posix_prefix
============
{'data': '/Users/dhellmann/Envs/pymotw',
 'include': '/Users/dhellmann/Envs/pymotw/include/python2.7',
 'platinclude': '/Users/dhellmann/Envs/pymotw/include/python2.7',
 'platlib': '/Users/dhellmann/Envs/pymotw/lib/python2.7/site-packages',
 'platstdlib': '/Users/dhellmann/Envs/pymotw/lib/python2.7',
 'purelib': '/Users/dhellmann/Envs/pymotw/lib/python2.7/site-packages',
 'scripts': '/Users/dhellmann/Envs/pymotw/bin',
 'stdlib': '/Users/dhellmann/Envs/pymotw/lib/python2.7'}

posix_user
==========
{'data': '/Users/dhellmann/Library/Python/2.7',
 'include': '/Users/dhellmann/Library/Python/2.7/include/python2.7',
 'platlib': '/Users/dhellmann/Library/Python/2.7/lib/python2.7/site-packages',
 'platstdlib': '/Users/dhellmann/Library/Python/2.7/lib/python2.7',
 'purelib': '/Users/dhellmann/Library/Python/2.7/lib/python2.7/site-packages',
 'scripts': '/Users/dhellmann/Library/Python/2.7/bin',
 'stdlib': '/Users/dhellmann/Library/Python/2.7/lib/python2.7'}

For an individual path, call get_path().

import sysconfig
import pprint

for scheme in ['posix_prefix', 'posix_user']:
    print scheme
    print '=' * len(scheme)
    print 'purelib =', sysconfig.get_path(name='purelib', scheme=scheme)
    print
    

Using get_path() is equivalent to saving the value of get_paths() and looking up the individual key in the dictionary. If you need several paths, get_paths() is more efficient because it does not recompute all of the paths each time.

$ python sysconfig_get_path.py

posix_prefix
============
purelib = /Users/dhellmann/Envs/pymotw/lib/python2.7/site-packages

posix_user
==========
purelib = /Users/dhellmann/Library/Python/2.7/lib/python2.7/site-packages

Python Version and Platform

While sys includes some basic platform identification (see Build-time Version Information), it is not specific enough to be used for installing binary packages because sys.platform does not always include information about hardware architecture, bit-ness, or other values that effect the compatibility of binary libraries. For a more precise platform specifier, use get_platform().

import sysconfig

print sysconfig.get_platform()

Although this sample output was prepared on an OS X 10.6 system, the interpreter is compiled for 10.5 compatibility, so that is the version number included in the platform string.

$ python sysconfig_get_platform.py

macosx-10.6-intel

As a convenience, the interpreter version from sys.version_info is also available through get_python_version() in sysconfig.

import sysconfig
import sys

print 'sysconfig.get_python_version() =>', sysconfig.get_python_version()
print 'sys.version_info =>', sys.version_info

get_python_version() returns a string suitable for use when building a version-specific path.

$ python sysconfig_get_python_version.py

sysconfig.get_python_version() => 2.7
sys.version_info => sys.version_info(major=2, minor=7, micro=2, releaselevel='final', serial=0)

See also

sysconfig
The standard library documentation for this module.
distutils
sysconfig used to be part of the distutils package.
distutils2
Updates to distutils, managed by Tarek Ziadé.
site
The site module describes the paths searched when importing in more detail.
os
Includes os.name, the name of the current operating system.
sys
Includes other build-time information such as the platform.