Check for existence of Python dev files from bash script

That would be pretty much how I would go about doing it. Seems reasonable simple.

However, if I need to be really sure that python-devel files are installed for the current version of Python, I would look for the relevant Python.h file. Something along the lines of:

# first, makes sure distutils.sysconfig usable
if ! $(python -c "import distutils.sysconfig.get_config_vars" &> /dev/null); then
    echo "ERROR: distutils.sysconfig not usable" >&2
    exit 2
fi

# get include path for this python version
INCLUDE_PY=$(python -c "from distutils import sysconfig as s; print s.get_config_vars()['INCLUDEPY']")
if [ ! -f "${INCLUDE_PY}/Python.h" ]; then
    echo "ERROR: python-devel not installed" >&2
    exit 3
fi

Note: distutils.sysconfig may not be supported on all platforms so not the most portable solution, but still better than trying to cater for variations in apt, rpm and the likes.

If you really need to support all platforms, it might be worth exploring what is done in the AX_PYTHON_DEVEL m4 module. This module can be used in a configure.ac script to incorporate checks for python-devel during the ./configure stage of an autotools-based build.


Imho your solutions works well.

Otherwise, a more "elegant" solution would be to use a tiny script like:

testimport.py

#!/usr/bin/env python2

import sys

try:
  __import__(sys.argv[1])
  print "Sucessfully import", sys.argv[1]
except:
  print "Error!"
  sys.exit(4)

sys.exit(0)

And call it with testimport.sh distutils.sysconfig

You can adapt it to check for internal function if needed...

Tags:

Python

Bash