0

This one kept throwing me when I started Django. Suppose you have a project project with an app app, and you want to inspect it. So you import parts of it in the Python shell:

(virtualenv) $ PYTHONPATH=. python
Python 3.9.2 (default, Feb 28 2021, 17:03:44) 
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import project.app.views
(...cut traceback...)
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS,
but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE
or call settings.configure() before accessing settings.

Fine. So you set DJANGO_SETTINGS_MODULE:

(virtualenv) $ PYTHONPATH=. DJANGO_SETTINGS_MODULE=project.project.settings python
Python 3.9.2 (default, Feb 28 2021, 17:03:44) 
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import project.app.views
(...cut traceback...)
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

with no explanation as to what to do next.

1 Answer 1

0

The answer is:

  1. Set DJANGO_SETTINGS_MODULE to project.project.settings (where project is the name of your project)
  2. In the Python shell, import django; django.setup() as the first thing you do.

So, for example:

(virtualenv) $ PYTHONPATH=. DJANGO_SETTINGS_MODULE=project.project.settings python
Python 3.9.2 (default, Feb 28 2021, 17:03:44) 
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import django; django.setup()
>>> import project.app.views

All good!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .