Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Imports From The Directory Above

I have a directory structure for my python application where in the main folder I have a folder called handlers and in that file I have a base.py that all handlers should inherit from . I also have a folder called users that contains all the handlers relating to users.

\main
      \handlers
      base.py
      __init__.py
      \users
                __init__.py
                authenticated.py
                logout.py   

My issue is I can't import the base in any file in the users folder but can import from the users folder. I know that I have to do something like bellow

from ..handlers import *    

But that does not work for some reason all I am trying to do is import base.py into files in the users directory and other directorys I add at a later date.

like image 805
bobthemac Avatar asked Jan 09 '14 20:01

bobthemac


1 Answers

To enable relative import, add main/__init__.py to make main directory into a Python package. The main's parent directory should be in sys.path.

like image 54
jfs Avatar answered Sep 19 '22 18:09

jfs