Updated On : Mar-02,2021 Time Investment : ~10 mins

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.

import sysconfig

Python Version


  • get_python_version() - This method returns Python version as Major:Minor string.

sysconfig.get_python_version()
'3.7'

Platform Details


  • 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.

sysconfig.get_platform()
'linux-x86_64'

Python Built from Source Code


  • is_python_build() - This method returns boolean value True if Python interpreter was built from source and is being run from its source location else returns False.

sysconfig.is_python_build()
False

Path to Python's Config File


  • get_config_h_filename() - This method returns path to Python's configuration file named pyconfig.h.

sysconfig.get_config_h_filename()
'/home/sunny/anaconda3/include/python3.7m/pyconfig.h'

Path to Makefile


  • get_makefile_filename() - This method returns path to Python's Makefile.

sysconfig.get_makefile_filename()
'/home/sunny/anaconda3/lib/python3.7/config-3.7m-x86_64-linux-gnu/Makefile'

Retrieve Value of Configuration Environment Variables


  • get_config_vars(args) - This method accepts python's configuration environment variables as input and returns their values.
  • get_config_var(name) - This method accepts single python's configuration environment variable as input and returns it's value.

sysconfig.get_config_vars("py_version")
['3.7.3']
sysconfig.get_config_vars("py_version", "prefix", "LIBDIR")
['3.7.3', '/home/sunny/anaconda3', '/home/sunny/anaconda3/lib']
sysconfig.get_config_var("py_version")
'3.7.3'

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.

sysconfig.get_scheme_names()
('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.

sysconfig.get_path_names()
('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.

for scheme in sysconfig.get_scheme_names():
    print("{:10s} - {}\n".format(scheme, sysconfig.get_paths(scheme)))
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.

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)))
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.

!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.

References

Sunny Solanki  Sunny Solanki

YouTube Subscribe Comfortable Learning through Video Tutorials?

If you are more comfortable learning through video tutorials then we would recommend that you subscribe to our YouTube channel.

Need Help Stuck Somewhere? Need Help with Coding? Have Doubts About the Topic/Code?

When going through coding examples, it's quite common to have doubts and errors.

If you have doubts about some code examples or are stuck somewhere when trying our code, send us an email at coderzcolumn07@gmail.com. We'll help you or point you in the direction where you can find a solution to your problem.

You can even send us a mail if you are trying something new and need guidance regarding coding. We'll try to respond as soon as possible.

Share Views Want to Share Your Views? Have Any Suggestions?

If you want to

  • provide some suggestions on topic
  • share your views
  • include some details in tutorial
  • suggest some new topics on which we should create tutorials/blogs
Please feel free to contact us at coderzcolumn07@gmail.com. We appreciate and value your feedbacks. You can also support us with a small contribution by clicking DONATE.


Subscribe to Our YouTube Channel

YouTube SubScribe

Newsletter Subscription