pokeymond
pokeymond

Reputation: 671

Cannot import module python ImportError

I have a project with this structure (showing only with the affected):

main.py # starting script
/app
  |__init__.py
  |/exceptions
    | __init__.py # content: from .ex400 import *
    | base.py
    | ex400.py # content: some classes extending classes from base.py
  |/extend
    | __init__.py # empty
    | module.py

from /app/extend/module.py, I trying to import classes from /app/exceptions/ex400.py. And from main I imported module.py

module.py

# I added this to check the path, output is below
import sys
print 'Working directory =>', sys.path

from app.exceptions.ex400 import SomeClass, MoreClass

# ... some code here ofcourse

And when running, I get an ImportError.

Working directory => ['/prj/myproject', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']

...
File "/prj/myproject/app/extend/module.py", line 5, in <module>
   from app.exceptions.ex400 import SomeClass, MoreClass
ImportError: No module named exceptions.ex400

Am I missing something here?

Upvotes: 3

Views: 180

Answers (1)

FMaz
FMaz

Reputation: 394

Yes, you are missing something. Its just something little but importing with Python is a pain in the a** anyways. For example you do not have an __init__.py in you app-folder. That should fix your issue. And by the way to not get confused over namespaces etc. I would simply leave the __init__.py in your exceptions-folder empty.

Cheers

Upvotes: 2

Related Questions