19

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
  • 8,814
  • 17
  • 70
  • 133

3 Answers3

37

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,608
  • 9
  • 28
  • 44
Dimitris Fasarakis Hilliard
  • 136,212
  • 29
  • 242
  • 233
12

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
  • 3,320
  • 1
  • 27
  • 43
9

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

Robᵩ
  • 154,489
  • 17
  • 222
  • 296
  • It works! Is there an analog for finding Python's lib? – kilojoules Jan 28 '16 at 20:24
  • 1
    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