25

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)"
0

3 Answers 3

48

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

Sign up to request clarification or add additional context in comments.

2 Comments

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"
This is clutch! I was losing my mind looking for this. sysconfig rules!
17

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

3 Comments

It works! Is there an analog for finding Python's lib?
Try python-config --help or python-config --libs.
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.
16

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\"])"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.