Nested/Sub Extensions in Python

Carl Banks pavlovevidence at gmail.com
Sat Jul 2 22:43:29 CEST 2011


On Saturday, July 2, 2011 6:35:19 AM UTC-7, H Linux wrote:
> On Jul 2, 2:28 am, Carl Banks 
>  wrote:
> > On Friday, July 1, 2011 1:02:15 PM UTC-7, H Linux wrote:
> > > Once I try to nest this, I cannot get the module to load anymore:
> > > >import smt.bar
> > > Traceback (most recent call last):
> > >   File "<stdin>", line 1, in <module>
> > > ImportError: No module named bar
> >
> > [snip]
> >
> > > PyMODINIT_FUNC
> > > initbar(void)
> > > {
> > >    Py_InitModule("smt.bar", bar_methods);
> > > }
> >
> > This should be: Py_InitModule("bar", bar_methods);
> > That's probably it; other than that, it looks like you did everything right.
> Thanks for your help, but I actually tried both ways. This does not
> seem to be the problem, as it fails both ways with identical error
> message.

Correct, I misspoke.  The problem would be if the initbar function name was misspelled.


> > What does the installed file layout look like after running distutils setup?
> Tree output is:
> /usr/local/lib/python2.6/dist-packages/
> ├── foo.so
> ├── smt
> │   ├── bar.so
> │   ├── __init__.py
> │   └── __init__.pyc
> └── smt-0.1.egg-info
> 
> Just in case anyone is willing to have a look, here is a link to the
> complete module as built with:
> python setup.py sdist:
> https://docs.google.com/leaf?id=0Byt62fSE5VC5NTgxOTFkYzQtNzI3NC00OTUzLWI1NzMtNmJjN2E0ZTViZTJi&hl=en_US
> 
> If anyone has any other ideas how to get it to work, thanks in
> advance...

I got and built the package, and it imported smt.bar just fine for me.

So my advice would be to rename all the modules.  My guess is that there is a conflict for smt and Python is importing some other module or package.  Is there a file called smt.py in your working directory?  Try doing this:

import smt
print smt.__file__

And see if it prints at the location where your smt module is installed.  If not, you have a conflict.

And if that is the problem, in the future be more careful to keep your module namespace clean.  Choose good, distinct names for modules and packages to lessen the risk of conflict.


Carl Banks



More information about the Python-list mailing list