Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python import module from parent package

I have the following directory structure

foo/
    __init__.py
    settings.py
    bar/
        __init__.py
        myfile.py

In myfile.py I have: import settings

I get the following error: ImportError: No module named settings, why? How can I efectively import the settings file from myfile.py

like image 446
danielrvt Avatar asked Jan 10 '13 02:01

danielrvt


2 Answers

From http://docs.python.org/2/tutorial/modules.html#intra-package-references :

from .. import settings

Hope it helps

like image 79
Adrián Avatar answered Oct 11 '22 08:10

Adrián


Here is another method that seems more clear:

In foo.__init__.py:

  __all__ = ['settings', ..(all other modules at 'foo' level you want to show)...]

In myfile.py:

# instead of "from .. import..." 
  from foo import settings 
  print settings.theThing
like image 34
fatumbi Avatar answered Oct 11 '22 08:10

fatumbi