I have a shell script which installs all required packages for a project. Some of those packages are Python 3 based and require to compile things and for this the Python sources need to be installed1 (e.g. the python3-dev package in common Debian-based distributions). Otherwise, unintelligible/misleading compile error messages will be displayed (missing header files) which is something I want to prevent.

Now I thought it might be best to try to detect whether the Python 3 sources are already installed and in case they aren't not even attempt to run pip but instead instruct the user to get the Python sources for his/her distribution and maybe also some instructions for the most common ones.

In short: How can I reliably detect whether the Python 3 header files are installed on various distributions (and maybe even in non-Linux environments going as far as Cygwin)?

1. The package is mypy and parts of the error messages include:

ast27/Parser/acceler.c:13:25: fatal error: pgenheaders.h: No such file or directory
compilation terminated.


 Command "/path/to/my/venv/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-x0e2zn8t/typed-ast/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-uythmwn1-record/install-record.txt --single-version-externally-managed --compile --install-headers /path/to/my/venv/include/site/python3.5/typed-ast" failed with error code 1 in /tmp/pip-build-x0e2zn8t/typed-ast/

  • Would a kind of find / -type f -name 'whatever is needed' or a kind of condition check like [[ -f file_required ]] provide a good info? – George Vasiliou Jan 28 '17 at 1:13

I found a solution, basically I just need to check for the existence of python3-config in the PATH (command -v python3-config >/dev/null), this signifies that here are Python 3 development files.

If I would want to go even further I could theoretically also use this tool to get the include directories which I could check for some of the particular header files required but I see no need for this.

BTW, applies to Python 2 and Python 2.7+ similarly (python2-config and python2.7-config respectively).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.