Reputation: 10931
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
Upvotes: 0
Views: 1006
Reputation: 5819
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
Upvotes: 1