开发者

How do you import a module on one level higher?

开发者 https://www.devze.com 2023-02-15 23:15 出处:网络
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:

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

精彩评论

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