CheckPython, CheckPythonHeaders and CheckPythonModule (Autoconf Functionality)

The CheckPython, CheckPythonHeaders and CheckPythonModule methods are not yet a part of SCons. The current code is viewable here. Adding CheckPython, CheckPythonModule and CheckPythonHeaders methods became a part of my Google Summer of Code project.

This functionality is also available for download in installation packages for Python Binary Builder.

The original patch was submitted by Gustavo Carneiro.

Multi-Platform Configuration is described in SCons User Guide.

CheckPython method

CheckPython(minver) method checks if a python interpreter is found matching a given minimum version.

minver should be a tuple, eg. to check for python >= 2.4.2 pass (2,4,2) as minver.

If detection is successful, env['PYTHON'] is defined to point to the python that matches the minimum version constraint. In addition, PYTHON_VERSION is defined as 'MAJOR.MINOR' (eg. '2.4') of the actual python version found. Finally, pythondir and pyexecdir are defined, and point to the site-packages directories (architecture independent and architecture dependent respectively) appropriate for this python version.

CheckPythonHeaders method

CheckPythonHeaders method checks for headers necessary to compile python extensions. If successful, CPPPATH is augmented with paths for python headers.

This test requires that CheckPython was previously executed and successful.

CheckPythonModule method

CheckPythonModule(modulename) method checks for the existence of module 'modulename'.

This test requires that CheckPython was previously executed and successful.

Example

Sample SConstruct file:

   1 env = Environment()
   2 conf = Configure(env)
   3 # We need python 2.2.2 or higher
   4 if conf.CheckPython((2,2,2)):
   5     if conf.CheckPythonHeaders():
   6         print 'Found Python header files in:', env['CPPPATH']
   7     else:
   8         print 'Python header files not found'
   9         Exit(1)
  10     if not conf.CheckPythonModule('mechanize'):
  11         print "Module 'mechanize' not found"
  12         Exit(1)
  13 else:
  14     print 'Python 2.2.2 or higher must be installed'
  15     Exit(1)
  16 conf.Finish()

Let's now run SCons:

$ scons
scons: Reading SConscript files ...
Checking for Python >= 2.2.2...(cached) yes
Checking for C header file Python.h... yes
Found Python header files in: ['/usr/include/python2.5']
Checking for python module mechanize...(cached) no
Module 'mechanize' not found