sysconfig - Access Python Configuration Details¶
Developers many times need to know underlying Python's configuration details. These details can help make decisions about software like whether it'll work with the current configuration of the system or not. It can also be used to guide users about what changes should be made to the configuration so that particular software/script designed in Python can work on their system. It can also be used to make many decisions inside the software as well based on Python environment variables.
To get access to underlying Python's configuration details, Python provides us with a module named sysconfig. As a part of this tutorial, we'll explain various methods available with sysconfig to find out Python's configuration details.
We'll start by importing sysconfig.
- get_platform() - This method returns a string representing underlying platform. It can have information like OS name, version, and architecture.
If you are interested in learning about the underlying details of the platform then check our tutorial on Python module platform which provides methods for it.
'/home/sunny/anaconda3/include/python3.7m/pyconfig.h'
'/home/sunny/anaconda3/lib/python3.7/config-3.7m-x86_64-linux-gnu/Makefile'
['3.7.3', '/home/sunny/anaconda3', '/home/sunny/anaconda3/lib']
Scheme Names¶
Python follows an installation scheme based on the underlying platform. It has different schemes for different platforms. Based on the scheme, the new libraries are installed in the particular paths on the system.
- get_scheme_names() - This method returns a list of schemes that are currently supported by Python.
Below is a list of schemes currently supported by Python
- nt - It's a scheme for windows platform.
- nt_user - It's a scheme for windows platform with user option.
- osx_framework_user - It's a scheme for Mac OS X platform.
- posix_home - It's a scheme for linux/unix platforms when a home option is used while installing.
- posix_prefix - It's a default scheme for linux/unix platforms.
- posix_user - It's a scheme for linux/unix platforms when a user option is used while installing.
('nt',
'nt_user',
'osx_framework_user',
'posix_home',
'posix_prefix',
'posix_user')
Scheme Path Names¶
Each scheme that was specified in the previous example has a list of paths that will be used when installing the particular library.
- get_path_names() - This method returns list of paths available for each scheme.
Below is an explanation for each path.
- stdlib - It's a path where libraries which are not platform-specific will be installed.
- platstdlib - It's a path where libraries which are platform-specific will be installed.
- purelib - It's a path where libraries that are site-specific and platform-specific will be installed.
- platlib - It's a path where libraries that are site-specific and non-platform-specific will be installed.
- include - It's a path for non-platform specific header files.
- scripts - It's a path for scripts.
- data - It's a path for data.
('stdlib', 'platstdlib', 'purelib', 'platlib', 'include', 'scripts', 'data')
Path per Scheme¶
- get_paths(scheme) - This method takes as input scheme name and returns a dictionary where the key is path name and value is actual path.
Below our code is looping through all schema names and retrieving path names to path mapping for all of them.
nt - {'stdlib': '/home/sunny/anaconda3/Lib', 'platstdlib': '/home/sunny/anaconda3/Lib', 'purelib': '/home/sunny/anaconda3/Lib/site-packages', 'platlib': '/home/sunny/anaconda3/Lib/site-packages', 'include': '/home/sunny/anaconda3/Include', 'platinclude': '/home/sunny/anaconda3/Include', 'scripts': '/home/sunny/anaconda3/Scripts', 'data': '/home/sunny/anaconda3'}
nt_user - {'stdlib': '/home/sunny/.local/Python37', 'platstdlib': '/home/sunny/.local/Python37', 'purelib': '/home/sunny/.local/Python37/site-packages', 'platlib': '/home/sunny/.local/Python37/site-packages', 'include': '/home/sunny/.local/Python37/Include', 'scripts': '/home/sunny/.local/Python37/Scripts', 'data': '/home/sunny/.local'}
osx_framework_user - {'stdlib': '/home/sunny/.local/lib/python', 'platstdlib': '/home/sunny/.local/lib/python', 'purelib': '/home/sunny/.local/lib/python/site-packages', 'platlib': '/home/sunny/.local/lib/python/site-packages', 'include': '/home/sunny/.local/include', 'scripts': '/home/sunny/.local/bin', 'data': '/home/sunny/.local'}
posix_home - {'stdlib': '/home/sunny/anaconda3/lib/python', 'platstdlib': '/home/sunny/anaconda3/lib/python', 'purelib': '/home/sunny/anaconda3/lib/python', 'platlib': '/home/sunny/anaconda3/lib/python', 'include': '/home/sunny/anaconda3/include/python', 'platinclude': '/home/sunny/anaconda3/include/python', 'scripts': '/home/sunny/anaconda3/bin', 'data': '/home/sunny/anaconda3'}
posix_prefix - {'stdlib': '/home/sunny/anaconda3/lib/python3.7', 'platstdlib': '/home/sunny/anaconda3/lib/python3.7', 'purelib': '/home/sunny/anaconda3/lib/python3.7/site-packages', 'platlib': '/home/sunny/anaconda3/lib/python3.7/site-packages', 'include': '/home/sunny/anaconda3/include/python3.7m', 'platinclude': '/home/sunny/anaconda3/include/python3.7m', 'scripts': '/home/sunny/anaconda3/bin', 'data': '/home/sunny/anaconda3'}
posix_user - {'stdlib': '/home/sunny/.local/lib/python3.7', 'platstdlib': '/home/sunny/.local/lib/python3.7', 'purelib': '/home/sunny/.local/lib/python3.7/site-packages', 'platlib': '/home/sunny/.local/lib/python3.7/site-packages', 'include': '/home/sunny/.local/include/python3.7', 'scripts': '/home/sunny/.local/bin', 'data': '/home/sunny/.local'}
Path per Scheme and Path¶
- get_path(path_name, scheme_name) - This method takes path name and schema name as input and returns the actual path for that scheme and pathname.
Below our code is looping through schema names and path names and retrieving the actual paths for each combination of both.
nt - stdlib : /home/sunny/anaconda3/Lib
nt - platstdlib : /home/sunny/anaconda3/Lib
nt - purelib : /home/sunny/anaconda3/Lib/site-packages
nt - platlib : /home/sunny/anaconda3/Lib/site-packages
nt - include : /home/sunny/anaconda3/Include
nt - scripts : /home/sunny/anaconda3/Scripts
nt - data : /home/sunny/anaconda3
nt_user - stdlib : /home/sunny/.local/Python37
nt_user - platstdlib : /home/sunny/.local/Python37
nt_user - purelib : /home/sunny/.local/Python37/site-packages
nt_user - platlib : /home/sunny/.local/Python37/site-packages
nt_user - include : /home/sunny/.local/Python37/Include
nt_user - scripts : /home/sunny/.local/Python37/Scripts
nt_user - data : /home/sunny/.local
osx_framework_user - stdlib : /home/sunny/.local/lib/python
osx_framework_user - platstdlib : /home/sunny/.local/lib/python
osx_framework_user - purelib : /home/sunny/.local/lib/python/site-packages
osx_framework_user - platlib : /home/sunny/.local/lib/python/site-packages
osx_framework_user - include : /home/sunny/.local/include
osx_framework_user - scripts : /home/sunny/.local/bin
osx_framework_user - data : /home/sunny/.local
posix_home - stdlib : /home/sunny/anaconda3/lib/python
posix_home - platstdlib : /home/sunny/anaconda3/lib/python
posix_home - purelib : /home/sunny/anaconda3/lib/python
posix_home - platlib : /home/sunny/anaconda3/lib/python
posix_home - include : /home/sunny/anaconda3/include/python
posix_home - scripts : /home/sunny/anaconda3/bin
posix_home - data : /home/sunny/anaconda3
posix_prefix - stdlib : /home/sunny/anaconda3/lib/python3.7
posix_prefix - platstdlib : /home/sunny/anaconda3/lib/python3.7
posix_prefix - purelib : /home/sunny/anaconda3/lib/python3.7/site-packages
posix_prefix - platlib : /home/sunny/anaconda3/lib/python3.7/site-packages
posix_prefix - include : /home/sunny/anaconda3/include/python3.7m
posix_prefix - scripts : /home/sunny/anaconda3/bin
posix_prefix - data : /home/sunny/anaconda3
posix_user - stdlib : /home/sunny/.local/lib/python3.7
posix_user - platstdlib : /home/sunny/.local/lib/python3.7
posix_user - purelib : /home/sunny/.local/lib/python3.7/site-packages
posix_user - platlib : /home/sunny/.local/lib/python3.7/site-packages
posix_user - include : /home/sunny/.local/include/python3.7
posix_user - scripts : /home/sunny/.local/bin
posix_user - data : /home/sunny/.local
Running sysconfig as a Script¶
Below we have explained how we can run sysconfig module as a script to retrieve total configuration details. Please make a NOTE that we have shortened output as it had many environment variables. When you run this, you'll see a long output with all environment variables.
Platform: "linux-x86_64"
Python version: "3.7"
Current installation scheme: "posix_prefix"
Paths:
data = "/home/sunny/anaconda3"
include = "/home/sunny/anaconda3/include/python3.7m"
platinclude = "/home/sunny/anaconda3/include/python3.7m"
platlib = "/home/sunny/anaconda3/lib/python3.7/site-packages"
platstdlib = "/home/sunny/anaconda3/lib/python3.7"
purelib = "/home/sunny/anaconda3/lib/python3.7/site-packages"
scripts = "/home/sunny/anaconda3/bin"
stdlib = "/home/sunny/anaconda3/lib/python3.7"
Variables:
ABIFLAGS = "m"
AC_APPLE_UNIVERSAL_BUILD = "0"
AIX_GENUINE_CPLUSPLUS = "0"
ANDROID_API_LEVEL = "0"
...
This ends our small tutorial explaining how we can retrieve Python's configuration details using sysconfig module. Please feel free to let us know your views in the comments section.
References¶