0

May someone please give me complete explanation about how this works:

While models.py and views.py are on the same directory on a django app, why should we use relative import:

app1:
  models.py
    # my models was defined here

  views.py
    from .models import *      # this works
    from app1.models import *  # this also works
    from models import *       # ---this one does not work ---

The same will happend with admin.py

If I import

  from .models import *

Then

python manage.py check

Everything is ok but with

from models import *



python manage.py check

ImportError: No module named 'models'

I'm using now python 3.4.x and I had not this problem with 2.7.x

Serjik
  • 10,543
  • 8
  • 61
  • 70

1 Answers1

1

It would help to know what version of Python you're using, but I would guess it's Python 3. To quote PEP 8:

Implicit relative imports should never be used and have been removed in Python 3.

I would recommend reading through the section of PEP 8 on imports, here: https://www.python.org/dev/peps/pep-0008/#imports

And if you want to read more about the topic, I would suggest PEP 328, which goes into far more detail about the rationale for absolute vs relative imports.

Here is an other link with a more clear description on python 3 relative imports:

Changes in import statement python3

Community
  • 1
  • 1
Joey Wilhelm
  • 5,729
  • 1
  • 28
  • 42
  • 1
    I found the best answer here: http://stackoverflow.com/questions/12172791/changes-in-import-statement-python3 – Serjik Oct 19 '15 at 17:39