1

In my app's "views.py" file I have the following import:

from .models import City

The import and the app work fine when launched on localhost.

In the same folder as "views.py" and "models.py" I have created another file "database_csv_imports.py" for making a one-time database import from .csv file.

If I would use the same line as above to import City model into "database_csv_imports.py" file, I would get an error:

Traceback (most recent call last):
  File "database_csv_imports.py", line 3, in <module>
    from .models import City
SystemError: Parent module '' not loaded, cannot perform relative import 

So I have changed relative import to absolute:

from models import City

This, however, throws another error:

Traceback (most recent call last):
  File "database_csv_imports.py", line 3, in <module>
    from models import City
  File "/home/bart/python_projects/javascript/models.py", line 19, in <module>
    class Listing(models.Model):
  File "/home/bart/python_projects/testenv/lib/python3.5/site-packages/django/db/models/base.py", line 100, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/home/bart/python_projects/testenv/lib/python3.5/site-packages/django/apps/registry.py", line 244, in get_containing_app_config
    self.check_apps_ready()
  File "/home/bart/python_projects/testenv/lib/python3.5/site-packages/django/apps/registry.py", line 127, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

I have looked at similar SO questions, but haven't found a solution: I am running all scripts inside virtualenv, my app is installed in "settings.py", SECRET_KEY is not empty, and both makemigrations and migrate are already applied. I also thought that the solution posted here: django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. file init might me an overkill in this case.

Can someone help me fix the import error?

barciewicz
  • 3,511
  • 6
  • 32
  • 72

1 Answers1

1

Python, and also Django, will fight you if you try to directly run scripts inside packages.

The best ways to accomplish it is either to use the entry_points feature of setup.py, or writing a custom django admin command (https://docs.djangoproject.com/en/2.0/howto/custom-management-commands/).

If you want to do it the hard way, these are the steps you'll need

  1. the python path needs to include the parent directory of your package. The best way to do this is to run pip install -e . from the directory containing your setup.py. If you don't have a setup.py (you really should!) and if you're using virtualenvwrapper the add2virtualenv /home/bart/python_projects command will do this for you. Otherwise you'll need to edit the PYTHONPATH environment variable.

  2. Imports in the file you want to execute cannot be relative. In your case that means, from javascript.models import City (you need to start at the package level, the directory that is on the PYTHONPATH).

  3. You need to set the DJANGO_SETTINGS_MODULE to point to your settings file.

  4. You need to configure django after importing it import django; django.setup(), this should go at the top of your database_csv_imports.py file, before importing models.

thebjorn
  • 26,297
  • 11
  • 96
  • 138