Selectively importing correct extension module version

Brian Quinlan brian at sweetapp.com
Sat Jul 7 12:27:57 EDT 2001


Hi all,

I'm writing a Python extension module that wraps a large C++ project.
For the Windows distribution, I was planning on wrapping my extension
module in a package to hide all of the nasty DLLs that I need i.e.

Pyana\
	__init__.py
	Pyana.pyd (my extension module)
	nasty1.dll
	nasty2.dll 
	...
	nastyn.dll

My next thought was that I should put both the Python20 and Python21
versions of my extension in the same module since it is tiny (40K)
compared to the rest of the package (2.6MB). So I wrote a new
__init__.py file: 

version_info = (0,1,0)
__version__ = "0.1.0"

import sys

major, minor, micro, releaselevel, serial = sys.version_info

if major == 2 and minor == 0:
    from Pyana20 import *
elif major == 2 and minor == 1:
    from Pyana21 import *
elif major < 2:
    raise ImportError("Pyana requires Python 2.0 or later to run")
elif major >= 2:
    raise ImportError("Pyana cannot be run on your Python version.")

Then I made a nice ifdef system in my extension to make my module
initializer work correctly i.e. initPyana(20|21)?. 

Finally I realized that I would have to change a lot of internal strings
to reflect the correct module name e.g.

XSLError = PyErr_NewException("Pyana(20|21)?.XSLError", PyanaError,
NULL);

Does this seem like a good idea? Right now I am not sure if the
additional coding effort justifies the nice packaging.





More information about the Python-list mailing list