S
Samuel
Hi,
Given the following directory structure:
---------
|-- Obj.py
|-- __init__.py
|-- foo
| |-- FooTest.py
| `-- __init__.py
`-- start1.py
---------
With the following content:
---------
$ cat Obj.py
class Obj(object):
pass
$ cat __init__.py
import Obj
__all__ = ['Obj']
$ cat foo/FooTest.py
from Obj import Obj
class FooTest(object):
pass
$ cat foo/__init__.py
from FooTest import FooTest
__all__ = ['FooTest']
$ cat start1.py
from foo import FooTest
x = FooTest()
print "Works!"
sab@pcf2245:~/test$
---------
The test works:
---------
$ python start1.py
Works!
$
---------
However, if the module is imported elsewhere, e.g. in the following
directory structure:
---------
test
|-- Obj.py
|-- __init__.py
|-- foo
| |-- FooTest.py
| `-- __init__.py
`-- start1.py
test2
`-- start2.py
---------
Where start2.py has the following content:
---------
$ cd test2
$ cat start2.py
import sys, os.path
sys.path.insert(0,
os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
import test
import test.foo
print 'Works!'
---------
It does not work:
---------
$ python start2.py
Traceback (most recent call last):
File "start2.py", line 4, in <module>
import test.foo
File "/home/sab/test/foo/__init__.py", line 1, in <module>
from FooTest import FooTest
File "/home/sab/test/foo/FooTest.py", line 1, in <module>
from Obj import Obj
ImportError: No module named Obj
Given the following directory structure:
---------
|-- Obj.py
|-- __init__.py
|-- foo
| |-- FooTest.py
| `-- __init__.py
`-- start1.py
---------
With the following content:
---------
$ cat Obj.py
class Obj(object):
pass
$ cat __init__.py
import Obj
__all__ = ['Obj']
$ cat foo/FooTest.py
from Obj import Obj
class FooTest(object):
pass
$ cat foo/__init__.py
from FooTest import FooTest
__all__ = ['FooTest']
$ cat start1.py
from foo import FooTest
x = FooTest()
print "Works!"
sab@pcf2245:~/test$
---------
The test works:
---------
$ python start1.py
Works!
$
---------
However, if the module is imported elsewhere, e.g. in the following
directory structure:
---------
test
|-- Obj.py
|-- __init__.py
|-- foo
| |-- FooTest.py
| `-- __init__.py
`-- start1.py
test2
`-- start2.py
---------
Where start2.py has the following content:
---------
$ cd test2
$ cat start2.py
import sys, os.path
sys.path.insert(0,
os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
import test
import test.foo
print 'Works!'
---------
It does not work:
---------
$ python start2.py
Traceback (most recent call last):
File "start2.py", line 4, in <module>
import test.foo
File "/home/sab/test/foo/__init__.py", line 1, in <module>
from FooTest import FooTest
File "/home/sab/test/foo/FooTest.py", line 1, in <module>
from Obj import Obj
ImportError: No module named Obj