22

I know that after installing Python via Homebrew my include directory is here:

/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/include/python2.7

Is there a way I can make Python tell me where its include/lib directories are? Something along the lines of:

python -c "import sys; print '\n'.join(sys.path)"
kilojoules
  • 9,768
  • 18
  • 77
  • 149

3 Answers3

42

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

xaav
  • 7,876
  • 9
  • 30
  • 47
Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
  • When I do this, it shows `'include': '/usr/include/python3.7m'`, but when I try to `cd` to that directory, it says "No such file or directory" – GooDeeJAY Nov 02 '22 at 17:51
15

On my PC, the command is python-config --includes. Make sure you use the python-config that homebrew installed, not the default one.

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • It works! Is there an analog for finding Python's lib? – kilojoules Jan 28 '16 at 20:24
  • 2
    Try `python-config --help` or `python-config --libs`. – Robᵩ Jan 28 '16 at 20:24
  • 1
    Some Python distributions (such as Anaconda) do not come with `python-config`. If `python-config` is used to determine include path name, then it may be pointing to the include path of the python came with your OS. – yoonghm Mar 29 '19 at 01:34
14

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\"])"
yoonghm
  • 4,198
  • 1
  • 32
  • 48