Python Sysconfig Module
The sysconfig module provides access to Python's configuration information, such as the list of installation paths and the configuration variables used to compile the Python interpreter.
Importing the Module
import sysconfig
Installation Paths
Python uses a scheme to determine where to install files. You can access these paths using sysconfig.
sysconfig.get_paths()
Returns a dictionary containing all installation paths for the current scheme.
import sysconfig
import pprint
paths = sysconfig.get_paths()
pprint.pprint(paths)
sysconfig.get_path(name)
Returns a specific path from the installation scheme. Common names include 'stdlib', 'platstdlib', 'purelib', 'platlib', 'include', 'scripts', 'data'.
import sysconfig
print(sysconfig.get_path('stdlib'))
print(sysconfig.get_path('scripts'))
Configuration Variables
The sysconfig module also provides access to the variables used to build Python (e.g., compiler flags, library locations).
sysconfig.get_config_vars()
Returns a dictionary of all configuration variables.
import sysconfig
# Get all variables
vars = sysconfig.get_config_vars()
print(f"Number of variables: {len(vars)}")
sysconfig.get_config_var(name)
Returns the value of a single variable.
import sysconfig
# Check if IPv6 is enabled
print(sysconfig.get_config_var('IPV6_ENABLE'))
# Get the library directory
print(sysconfig.get_config_var('LIBDIR'))
Platform Information
sysconfig.get_platform()
Returns a string identifying the current platform.
import sysconfig
print(sysconfig.get_platform())
# Output example: linux-x86_64
sysconfig.get_python_version()
Returns the Python version string (MAJOR.MINOR).
import sysconfig
print(sysconfig.get_python_version())
# Output example: 3.10