Bob Rockefeller
Bob Rockefeller

Reputation: 4616

ModuleNotFoundError attempting to load development settings

I'm very new to Django, so please excuse my rookie question. I understand the need to separate the development configuration from the production one. And so I have created a folder at br_project/br_project/settings/ and in it I have these files:

- __init__.py
- base.py
- development.py
- production.py

The br_project/br_project/__init__.py file contains:

import os

env = os.environ.get('DJANGO_ENV', 'local')

if env == 'production':
  from .production import *
else:
  from .development import *

When .development is called, I get an error: ModuleNotFoundError: No module named 'br_project.production'

Somehow my configuration files are not being found. What have I set up wrong?

Upvotes: 1

Views: 44

Answers (1)

Tarquinius
Tarquinius

Reputation: 1948

Your import misses the settings directory. Given the directory structure below:

br_project/
├── br_project/
│   ├── __init__.py
│   ├── settings/
│   │   ├── __init__.py
│   │   ├── base.py
│   │   ├── development.py
│   │   └── production.py

you would want to import like this:

# br_project/br_project/__init__.py
import os

env = os.environ.get('DJANGO_ENV', 'local')

if env == 'production':
    from .settings.production import *
else:
    from .settings.development import *

Alternative:

Above should fix your error. Anyhow the following allows you to point explicitly to a settings file:

The definition which settings you choose is normally set by the environment variable DJANGO_SETTINGS_MODULE. This you can obviously EXPORT in bash before you run python manage.py runserver but you can also include it via

python manage.py runserver --settings=br_project.settings.production

This allows you to explicitly point to a settings file. You'd then write in 'production.py' from .base import * and the 'br_project/br_project/init.py' you can leave emtpy. Quick but good read: https://docs.djangoproject.com/en/5.1/topics/settings/

Upvotes: 1

Related Questions