Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to import module

Tags:

python

So first... my directory structure..

---script/execute.py
          |
          L---helper---foo.py
                 L-----bar.py
                 L--- __init__.py

Now, execute.py calls both foo and bar .py as

from helper.foo import some_func

I am trying to run this as:

python script/execute.py 

But I am getting this import error

  from helper.foo import some_func
  Import error: No module named helper??

What am I missing (note that there is no init inside script folder??)?

Thanks

like image 920
frazman Avatar asked Nov 01 '22 12:11

frazman


1 Answers

You should check out http://docs.python.org/2/tutorial/modules.html#packages

The "too long, didn't read" of it is that you need to have a file called __init__.py in your helper directory, e.g.,

$ touch helper/__init__.py

The file can also contain Python code, but in the simplest form an empty file is ok.

like image 199
Daniel Landau Avatar answered Nov 15 '22 03:11

Daniel Landau