How to find out where the Python include directory is?
On my PC, the command is python-config --includes
. Make sure you use the python-config
that homebrew installed, not the default one.
My one-line solution is
python -c "from sysconfig import get_paths as gp; print(gp()['include'])"
If you would like to embed the code within a Unix shell (such as bash), you have to use escaped double quotations.
python -c "from sysconfig import get_paths as gp; print(gp()[\"include\"])"
There must be an easier way to do this from Python, I thought, and there is, in the standard library of course. Use get_paths
from sysconfig
:
from sysconfig import get_paths
from pprint import pprint
info = get_paths() # a dictionary of key-paths
# pretty print it for now
pprint(info)
{'data': '/usr/local',
'include': '/usr/local/include/python2.7',
'platinclude': '/usr/local/include/python2.7',
'platlib': '/usr/local/lib/python2.7/dist-packages',
'platstdlib': '/usr/lib/python2.7',
'purelib': '/usr/local/lib/python2.7/dist-packages',
'scripts': '/usr/local/bin',
'stdlib': '/usr/lib/python2.7'}
You could also use the -m
switch with sysconfig
to get the full output of all configuration values.
This should be OS/Python version agnostic, use it anywhere. :-)