Skip to content
Mats Wichmann edited this page May 29, 2021 · 2 revisions

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:

env = Environment()
conf = Configure(env)
# We need python 2.2.2 or higher
if conf.CheckPython((2,2,2)):
    if conf.CheckPythonHeaders():
        print 'Found Python header files in:', env['CPPPATH']
    else:
        print 'Python header files not found'
        Exit(1)
    if not conf.CheckPythonModule('mechanize'):
        print "Module 'mechanize' not found"
        Exit(1)
else:
    print 'Python 2.2.2 or higher must be installed'
    Exit(1)
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
Clone this wiki locally