How do you import a module on one level higher?
For:
app/
__init__.py
abc.py
mod/
__init__.py
def.py
How would I import abc.py from开发者_如何学Python def.py?
to import module 'abc.py' that is in the parent directory of your current module:
import os
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
os.sys.path.insert(0,parentdir)
import abc
in module def if you want to import say abc just do:
from ..abc import *
Note: as def is a python keyword, using that name for a module does not sound like a good idea.
精彩评论