Writing Portable Code
Prev | Chapter 3. User's Guide | Next

## Writing Portable Code

For many modules PyOpenGL is designed to support multiple versions of the API exposed by that particular module. For instance, the `GLU` module supports GLU API versions 1.0, 1.1, 1.2 and 1.3. In many cases different versions of an API have different functions and constants. There are several ways for Python code to determine at runtime which API is exposed.

All PyOpenGL modules have an attribute named `__api_version__` which is set to an integer which defines the current version. Usually this a combination of the major version number in the high word and the minor in the low word. So `GLU` has

    __api_version__ = 0x100 | 0x101 | 0x102 | 0x103

Currently the only exception to this is the `GLUT` module which only uses the low word to store the Xlib implementation number (see your GLUT header for information about this.)

Often times a C header which defines an API has some macro or macros which define the version number. PyOpenGL includes the same macros as module attributes to make porting C code easier. For the `GLU` module these macros are `GLU_VERSION_1_1`, `GLU_VERSION_1_2`, and `GLU_VERSION_1_3`. So if your Python code needs GLU 1.2 to run then you could test for this at runtime by doing the following:

    from OpenGL.GLU import *

    try:
        GLU_VERSION_1_2
    except:
        print "Help! I'm lost without GLU 1.2!"
        sys.exit(1)

Prev | Home | Next
Extensions | Up | Chapter 4. Internals