0

I encountered this error when I tried to run my project. Line of code producing this error is:

from DomainServices.models.movie import Movie

But I have Movle class in my model folder and movie.py file. I also have an __init__.py containing:

__author__ = 'ehsan'

As I've read here: Python error "ImportError: No module named" I'm sure the problem is not about init.py contents, because I replaced this file with a working one from another project. Whole error is:

[root@h149-3-137-195 DomainServices]$ python DomainServices/cron/imdb_crawler.py
Traceback (most recent call last):
  File "DomainServices/cron/imdb_crawler.py", line 6, in <module>
    from DomainServices.models.movie import Movie
ImportError: No module named DomainServices.models.movie

This is my project structure: ImportError: No module named

In the project directory when I run 'python' and run that import command, there is no problem! What should I do?

Community
  • 1
  • 1
ehsan shirzadi
  • 4,709
  • 16
  • 69
  • 112
  • What's your `PYTHONPATH`? You may need to add `.` into it (as well as an empty `__init__.py` inside `DomainServices/DomainServices`, as @ThiefMaster pointed out in their answer). – James Aylett Sep 07 '14 at 12:36
  • @JamesAylett In the project directory when I run 'python' and run that import command, there is no problem! – ehsan shirzadi Sep 07 '14 at 13:54
  • If your project structure above is up to date then you still don't have an `__init__.py` in the `DomainServices` directory, which is needed to make `DomainServices` a module. – James Aylett Sep 09 '14 at 08:34

1 Answers1

6

You should make DomainServices a package by creating a __init__.py in there and then use absolute imports:

from DomainServices.models.movie import Movie
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636