In a Python package directory of my own creation, I have an __init__.py
file that says:
from _foo import *
In the same directory there is a _foomodule.so which is loaded by the above. The shared library is implemented in C++ (using Boost Python). This lets me say:
import foo
print foo.MyCppClass
This works, but with a quirk: the class is known to Python by the full package path, which makes it print this:
foo._foo.MyCppClass
So while MyCppClass
exists as an alias in foo
, foo.MyCppClass
is not its canonical name. In addition to being a bit ugly, this also makes help()
a bit lame: help(foo)
will say that foo contains a module _foo
, and only if you say help(foo._foo)
do you get the documentation for MyCppClass
.
Is there something I can do differently in __init__.py
or otherwise to make it so Python sees foo.MyCppClass
as the canonical name?
I'm using Python 2.7; it would be great if the solution worked on 2.6 as well.
I had the same problem. You can change the module name in your Boost.Python definition:
The help issue is a separate problem. I think you need to add each item to
__all__
to get it exported to help.When I do both of these, the name of
foo.MyCppClass
is just that --foo.MyCppClass
-- andhelp(foo)
gives documentation forMyCppClass
.