How do I install a Python extension module using distutils?

I am working on a Python package named "lehmer" which includes a bunch of plugins written in C. I currently have one "rng" plugin. I am using Python Distutils to build and install a module. I can compile and install the module, but when I try to import the module with import lehmer.rng

or from lehmer import rng

, the Python interpreter throws an exception ImportError

. I can import "lemer" ok.

Here is the content of my file setup.py

:

from distutils.core import setup, Extension

exts = [Extension("rng", ["lehmer/rng.c"])]

setup(name="lehmer",
      version="0.1",
      description="A Lehmer random number generator",
      author="Steve Park, Dave Geyer, and Michael Dippery",
      maintainer="Michael Dippery",
      maintainer_email="mpd@cs.wm.edu",
      packages=["lehmer"],
      ext_package="lehmer",
      ext_modules=exts)

      

When I list the contents of the Python directory site-packages

, I see the following:

th107c-4 lehmer $ ls /scratch/usr/lib64/python2.5/site-packages/lehmer
__init__.py  __init__.pyc  rng.so*

      

My environment variable is PYTHONPATH

set correctly, so not a problem (and as noted earlier, I can import lehmer

just fine, so I know it is PYTHONPATH

not a problem). Python uses the following search paths (as reported sys.path

):

['', '/scratch/usr/lib64/python2.5/site-packages', '/usr/lib/python25.zip', '/usr/lib64/python2.5', '/usr/lib64/python2.5/plat-linux2', '/usr/lib64/python2.5/lib-tk', '/usr/lib64/python2.5/lib-dynload', '/usr/lib64/python2.5/site-packages', '/usr/lib64/python2.5/site-packages/Numeric', '/usr/lib64/python2.5/site-packages/PIL', '/usr/lib64/python2.5/site-packages/SaX', '/usr/lib64/python2.5/site-packages/gtk-2.0', '/usr/lib64/python2.5/site-packages/wx-2.8-gtk2-unicode', '/usr/local/lib64/python2.5/site-packages']

      

Update

It works when used on OpenSUSE 10, but C extensions still fail to load when tested on Mac OS X. Below are the results from the Python interpreter:

>>> sys.path
['', '/usr/local/lib/python2.5/site-packages', '/opt/local/lib/python25.zip', '/opt/local/lib/python2.5', '/opt/local/lib/python2.5/plat-darwin', '/opt/local/lib/python2.5/plat-mac', '/opt/local/lib/python2.5/plat-mac/lib-scriptpackages', '/opt/local/lib/python2.5/lib-tk', '/opt/local/lib/python2.5/lib-dynload', '/opt/local/lib/python2.5/site-packages']
>>> from lehmer import rng
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name rng
>>> import lehmer.rngs
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named rngs
>>> import lehmer.rng 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named rng
>>> from lehmer import rngs
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name rngs

      

+1


source to share


1 answer


For the record (and because I'm tired of seeing this marked as unanswered), there were issues here:



  • Since the current directory is automatically added to the Python package path, the interpreter first looks for packages in the current directory; since some C modules were not compiled in the current directory, the interpreter could not find them. Solution: Do not start the interpreter from the same directory as your working copy of the code.
  • Distutils did not install the module with the correct permissions on OS X. Solution: Correct the permissions.
+3


source







All Articles