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()
This looks good to me. You could forgo the package approach (ie remove the __init__.py
file) and use relative imports instead, but I honestly like your approach better.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.