I want to have a Python script build a Python C extension module for whichever version of the Python interpreter with which the script is being run.
To this purpose, I want to dynamically acquire the relevant C compiler flags – one way to accomplish this would be to grab the output of the python-config
CLI tool, e.g. like so:
subprocess.check_output(["python-config", "--includes"])
Unfortunately, on systems on which several versions of Python are installed, this method may not find the right python-config
– that is to say, the python-config
associated with the currently-running version of Python. (Also, it is a bit ugly, methodologically.)
Is there some way that I can acquire this information from within Python – something perhaps like Numpy’s get_include
?
I have the answer: it is the built-in sysconfig
module.
… offering methods to access the same information python-config
provides, including but not limited to:
get_python_version()
(which does what you think),get_paths()
(yields a dict
with many useful paths),get_config_var()
and get_config_vars()
(these return strings and a huge dict
respectively, to sift through all of the low-level configuration options),get_config_h_filename()
(yields the source config.h
file),parse_config_h()
(for only the most intrepid configuration plumbers)… the first two of which will likely suffice, for anyone seeking python-config
values without having to go through an embarrassing subprocess
dance to get it.