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.
import sysconfig
sysconfig.get_python_version()
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.
sysconfig.get_platform()
sysconfig.is_python_build()
sysconfig.get_config_h_filename()
sysconfig.get_makefile_filename()
sysconfig.get_config_vars("py_version")
sysconfig.get_config_vars("py_version", "prefix", "LIBDIR")
sysconfig.get_config_var("py_version")
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.
Below is a list of schemes currently supported by Python
sysconfig.get_scheme_names()
Each scheme that was specified in the previous example has a list of paths that will be used when installing the particular library.
Below is an explanation for each path.
sysconfig.get_path_names()
Below our code is looping through all schema names and retrieving path names to path mapping for all of them.
for scheme in sysconfig.get_scheme_names():
print("{:10s} - {}\n".format(scheme, sysconfig.get_paths(scheme)))
Below our code is looping through schema names and path names and retrieving the actual paths for each combination of both.
for scheme in sysconfig.get_scheme_names():
for path in sysconfig.get_path_names():
print("{:18s} - {:12s} : {}".format(scheme, path, sysconfig.get_path(path,scheme)))
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.
!python -m sysconfig
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.