Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import __init__'s method in inner module?

Tags:

python

import

My structure of file is

foo/
   __init__.py
   bar.py

file __init__.py

def abc():
    print 'ABC'

file bar.py

from foo import abc

I got this error.

Traceback (most recent call last):
   File "foo/bar.py", line 1, in <module>
   from foo import abc
ImportError: No module named foo
like image 952
Nilesh Avatar asked Jul 25 '11 09:07

Nilesh


1 Answers

Use a relative import (requires Python 2.6 or greater):

from . import abc
like image 199
Chris Morgan Avatar answered Oct 05 '22 07:10

Chris Morgan