I am learning django, following on "Tango with Django"
My project structure is as follows:
/django_demo
|-- __init__.py
|-- manage.py
|-- populate.py
|-- settings.py
|.....<other modules>
|
|--/django_demo
| |-- __init__.py
| |-- populate_rango.py
| |...<other modules>
|
|--/rango
| |-- __init__.py
| |-- models.py
| |-- apps.py
| |....<other modules>
I am not able to resolve two queries:
Query 1 -
populate_rango.py
contains the following code:
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_demo.settings')
import django
django.setup()
from django_demo.rango import models
the models
import is giving unresolved import
error, even though I am using the correct absolute imports notation, acc. to the documentation, and similar questions on sibling package imports as well as [here][3]
Query 2 -
the populate.py
under the root /django-demo
package contains the following code:
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_demo.settings')
import django
django.setup()
from rango import models
from the documentation, it is mandatory to either use the absolute imports or the dotted notation to import from sub-folder.
the above piece of code uses neither. I am not able to understand how does the
from rango import models
doesn't throw an unresolved import error.
note 1 : I have already my appname included in the INSTALLED_APPS
under \django_demo\settings.py
as
INSTALLED_APPS = [
'rango.apps.RangoConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles', ]
the \rango\apps.py
has the RangoConfig
class:
class RangoConfig(AppConfig):
name = 'rango'
Note 2:
1. all my __init__.py
files are empty
- I am using virtualenv with python 3.5.2 as an interpreter on pydev+eclipse oxygen on Ubuntu
UPDATE-1
As seen in the traceback below, it is the django.setup()
which is giving a runtime error:
Traceback (most recent call last):
File "/home/sat/coding/python_dev/oxygen_workspace/django_demo/django_demo/populate_rango.py", line 10, in <module>
django.setup()
File "/home/sat/coding/flask_dev/venv3/lib/python3.5/site-packages/django/__init__.py", line 22, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "/home/sat/coding/flask_dev/venv3/lib/python3.5/site-packages/django/conf/__init__.py", line 56, in __getattr__
self._setup(name)
File "/home/sat/coding/flask_dev/venv3/lib/python3.5/site-packages/django/conf/__init__.py", line 41, in _setup
self._wrapped = Settings(settings_module)
File "/home/sat/coding/flask_dev/venv3/lib/python3.5/site-packages/django/conf/__init__.py", line 110, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/home/sat/coding/flask_dev/venv3/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named 'django_demo'
I do not understand the reason for this.
UPDATE-2
i created a new django project named new_django on similar lines, and the from rango import models
is working fine in that.
new_django structure:
/new_django
|-- __init__.py
|-- manage.py
|-- populate.py
|-- settings.py
|.....<other modules>
|
|--/new_django
| |-- __init__.py
| |-- populate_rango.py
| |...<other modules>
|
|--/rango
| |-- __init__.py
| |-- models.py
| |-- apps.py
| |....<other modules>
the populate_rango.py
contains the following code:
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'new_django.settings')
import django
django.setup()
from rango.models import
this terminates without any error when run.
the settings.py
and rango/apps.py
for new_django project have identical code as their django_demo counterparts.
When, however, I use the absolute import like from new_django.rango import models
, to refer to the hierarchy new_django/rango
as mentioned in documentation, I get the traceback below:
Traceback (most recent call last):
File "/home/sat/coding/python_dev/oxygen_workspace/new_django/new_django/populate_rango.py", line 12, in <module>
from new_django.rango import models
ImportError: No module named 'new_django.rango'
this also happens in case of relative import - from ..rango import models
:
Traceback (most recent call last):
File "/home/sat/coding/python_dev/oxygen_workspace/new_django/new_django/populate_rango.py", line 12, in <module>
from ..rango import models
SystemError: Parent module '' not loaded, cannot perform relative import
Could someone please explain why:
- in UPDATE-1,
django.setup()
is giving error? - in UPDATE-2, the import
from rango import models
is not giving an error, while absolute and relative imports are?