Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out where the Python include directory is?

Tags:

python

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)"
like image 277
kilojoules Avatar asked Jan 28 '16 20:01

kilojoules


People also ask

Where does Python find packages?

When a package is installed globally, it's made available to all users that log into the system. Typically, that means Python and all packages will get installed to a directory under /usr/local/bin/ for a Unix-based system, or \Program Files\ for Windows.

How do I add a directory to a path in Python?

Clicking on the Environment Variables button o​n the bottom right. In the System variables section, selecting the Path variable and clicking on Edit. The next screen will show all the directories that are currently a part of the PATH variable. Clicking on New and entering Python's install directory.


1 Answers

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

like image 129
Dimitris Fasarakis Hilliard Avatar answered Oct 03 '22 13:10

Dimitris Fasarakis Hilliard