views:

84

answers:

3

Here is the directory structure:

app/
    __init__.py
    sub1/
        __init__.py
        mod1.py
    sub2/
        __init__.py
        sub2.so
        test_sub2.py
  • The folder app is on my PYTHONPATH
  • All of the _init_.py files are empty.
  • The shared library sub2.so is a C++ extension module that I compiled using cmake and boost-python.
  • test_sub2.py is a test script for the class defined in sub2.so.
  • If I run test_sub2.py from the sub2 directory, it imports the module correctly and the test passes.

How do I import the class A from sub2.so into mod1.py?

+2  A: 

The way to import it is to import app.sub2.sub2, from any source file. Your test should actually live outside of app and use that module-path to get to the extension module.

Thomas Wouters
A: 

Try

import .app.sub2.sub2 

in your mod1.py file

Pavlo
A: 

Use relative imports:

from ..sub2.sub2 import A

This is similar to a relative path "../sub2/sub2.so".

AndiDog