开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜