sysconfig
The Python sysconfig
module provides access to Python’s configuration information, which includes details about the installation paths, the build environment, and platform-specific settings.
This module is beneficial for determining the environment in which your Python code is running and for making decisions based on that information.
Here’s a quick example:
>>> import sysconfig
>>> sysconfig.get_platform()
'macosx-15.3-arm64'
Key Features
- Retrieves Python’s installation paths
- Provides details about the build environment
- Provides access to platform-specific configuration data
- Retrieves compiler and linker flags used to build Python
- Lists available installation schemes for customization
Frequently Used Classes and Functions
Object | Type | Description |
---|---|---|
sysconfig.get_platform() |
Function | Returns a string identifying the current platform |
sysconfig.get_paths() |
Function | Returns a dictionary of standard paths |
sysconfig.get_path() |
Function | Returns the path for a specific scheme key |
sysconfig.get_config_var() |
Function | Retrieves the value of a single configuration variable |
sysconfig.get_path_names() |
Function | Returns a list of available path scheme keys |
Examples
Get standard installation paths:
>>> import sysconfig
>>> sysconfig.get_paths()
{
'stdlib': '/usr/local/lib/python3.13',
'platstdlib': '/usr/local/lib/python3.13',
...
}
Access a specific configuration variable:
>>> sysconfig.get_config_var("LIBDIR")
'/usr/local/lib'
List available path names:
>>> sysconfig.get_path_names()
[
'stdlib',
'platstdlib',
'purelib',
'platlib',
'include',
'platinclude',
'scripts',
'data'
]
Common Use Cases
- Determining the platform for platform-specific logic
- Locating Python’s installation paths for custom installations or deployments
- Accessing build-time configuration variables for extensions or tools
- Integrating with packaging tools to install files in the correct environment
- Querying or overriding install schemes for advanced deployment scenarios
Real-World Example
If you need to compile a C extension or interface with Python’s C API, you’ll want to locate include/
and library/
directories for your current Python installation:
import sysconfig
from pathlib import Path
# Get Python's include directory (for compiling C extensions)
include_dir = Path(sysconfig.get_path("include"))
plat_include_dir = Path(sysconfig.get_path("platinclude"))
# Get library and platform information
library_dir = sysconfig.get_config_var("LIBDIR")
python_lib = sysconfig.get_config_var("LIBRARY")
print("Include directory:", include_dir)
print("Platform-specific include directory:", plat_include_dir)
print("Library directory:", library_dir)
print("Python library:", python_lib)
This example uses sysconfig
to extract the include paths and library information needed when compiling a C extension against the current Python installation.
Related Resources
Tutorial
Building a Python C Extension Module
In this tutorial, you'll learn how to write Python interfaces in C. Find out how to invoke C functions from within Python and build Python C extension modules. You'll learn how to parse arguments, return values, and raise custom exceptions using the Python API.
By Leodanis Pozo Ramos • Updated July 23, 2025