Reputation: 6616
I want to be able to reference a top level module inside another module (one level deeper). My solution works, but I want to know if this is the best way to do it. Please suggest for minimum python 2.4
In below example I want to import base.py
in abc.py
.
Here is a simplified directory structure -
- test.py
- mymodule/
- base.py
- __init__.py
- meta/
- abc.py
- __init__.py
Contents of base.py
class basec1:
def basec1f1(self):
print "base::C1::F1() called"
class basec2:
def basec2f1(self):
print "base::C2::F1() called"
Contents of abc.py
from mymodule.base import basec1
def abcfn():
print "abcfn() called"
obj = basec1()
obj.basec1f1()
Contents of test.py
import mymodule.meta.abc as abc
abc.abcfn()
Upvotes: 1
Views: 1681
Reputation: 8332
This looks good to me. You could forgo the package approach (i.e. remove the __init__.py
file) and use relative imports instead, but I honestly like your approach better.
Upvotes: 1