How do you import a module one level up?

For:

app/
__init__.py
abc.py
  mod/
  __init__.py
  def.py

How do I import abc.py from def.py?

+3
source share
2 answers

to import the module 'abc.py', which is located 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
+7
source

in the def module, if you want to import say abc, just do:

from ..abc import *

Note: since def is a python keyword, using this name for a module does not seem like a good idea.

+2
source

All Articles