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 'Some highlights:'

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

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

Found 517 configuration settings

Some highlights:

  Installation prefixes:

  Version info:

  Base directories:

  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

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

Base directories:

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

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

$ python


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


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)

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

$ python

{'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'}

{'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)

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

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

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


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() => 2.7
sys.version_info => sys.version_info(major=2, minor=7, micro=2, releaselevel='final', serial=0)

See also

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