1

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

  1. 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:

  1. in UPDATE-1,django.setup() is giving error?
  2. in UPDATE-2, the import from rango import models is not giving an error, while absolute and relative imports are?
satvik.t
  • 423
  • 4
  • 19

1 Answers1

0

Since the Django version is 1.11 which you are using and having manage.py file inside the django_demo project the project directory is loaded in the path variable visible for any imports here on.

Query 2 It is running fine because the rango module which is present in the scope of the parent django_demo i.e. project folder loaded in the PATH. Query 1 It is showing import error since you have 2 module with same name django_demo causing double import problem See here.
So to solve this move your manage.py outside the parent project directory django_demo. This way your relative import will work smoothly as same name module will not conflict anymore.

|-- manage.py
/django_demo
    |-- __init__.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>  

Now we can import models.py file from rango module which is one level outside the parent directory in populate_rango.py file by

from django_demo.rango import models  
Rajan Chauhan
  • 1,378
  • 1
  • 13
  • 32
  • this is not how I named it, it is a standard practice followed in a Django project, to have a **parent folder** containing the *project* (of the same name) as well as the all the *app* folders. I only added an `__init__.py` for the parent `/django_new` folder, to avoid the `not a package` error while importing, so as to focus on the `import` error at hand – satvik.t Jul 30 '17 at 10:07
  • @satvik.t Could you please tell the dango package version in use. – Rajan Chauhan Jul 31 '17 at 07:44
  • it is Django 1.11, python 3.5.2 – satvik.t Jul 31 '17 at 08:47
  • @satvik.t You have a double import problem because of the version of django you are using is < 1.4 – Rajan Chauhan Jul 31 '17 at 08:54