Version Problem

Daniel Dittmar daniel.dittmar at sap.com
Thu Aug 7 06:29:59 EDT 2003


Thorsten Bischoff wrote:
> Hi all!
>
> I have created a python extension module written in C. Now when the
> last release change took place, the module produces import errors
> under windows and some warnings under linux. As far as I understand
> the C to Python API has changed and so the module has to be relinked.
> But there I see a problem: If I have to relink and recompile my
> module every time there is a new python release, there will be no
> time left to do the very important things.
>
> Which is the recommanded way to deal with different python versions
> and extension modules? I'm looking for some easy-to-use method,
> preventing me from maintaining my module for different python
> versions and which gives the user the most flexibillity. That means I
> do not want either to enjoin on the user which python version to use
> nor to maintain many different versions of my module.

The recommended way is of course distutils. But you'd still have to provide
binaries at least for Windows users, so you have to build them all.

What I did:
- write a makefile where the location of the Python includes, the output
directory of the object files and the linked extension are all configurable
via make variables
- this allows to build the extension for all the versions in one directory
while sharing object files that are not related to the Python version
- I pack them into the following structure:
  mypackage/__init__.py
  mypackage/mymod.py
  mypackage/python15/mymodmodule.so
  mypackage/python20/mymodmodule.so
  ..

mymod.py contains code like the following:

    import sys
    _pythonVersion = sys.version [:3]

    if _pythonVersion == "2.0":
        from python20.mymod import *
    elif _pythonVersion == "1.5":
        from python15.mymod import *
    else:
        raise ImportError

That way, I can have one download per platform.

> Besides: Does anybody know a good method for GNU autoconf to check
> which python version is installed?

Not autoconf, but you can use the preprocessor symbols PY_MAJOR_VERSION etc.
should your extension support additional features for newer Python versions.

Daniel







More information about the Python-list mailing list