Search code examples
djangopython-moduledjango-settingspythonpathdjango-apps

Django: How to tell Django where it should look for apps?


After reading Two Scoops of Django 1.11, I changed my project's structure like this:

myname_project
  ├── config/
  │   ├── settings/
  │   │   ├── base.py
  │   │   ├── local.py
  │   │   ├── staging.py
  │   │   ├── prod.py
  │   ├── __init__.py
  │   ├── urls.py
  │   └── wsgi.py
  ├── docs/
  ├── myname/
  │   ├── accounts/  # App
  │   ├── blog/ # App
  │   ├── core/ # App
  │   ├── media/ # Development only!
  │   ├── static/
  │   └── templates/
  ├── .gitignore
  ├── Makefile
  ├── README.md
  ├── manage.py
  └── requirements/

My problem is now, that Django doesn't find the app anymore. Here is a gist of how my settings files look:

import os
from pathlib import Path

from django.core.exceptions import ImproperlyConfigured


def get_env_variable(var_name):
    """Get the environment variable or return exception."""
    try:
        return os.environ[var_name]
    except KeyError:
        error_msg = 'Set the {} environment variable'.format(var_name)
        raise ImproperlyConfigured(error_msg)

# Build paths inside the project like this: BASE_DIR / 'media'
BASE_DIR = Path(__file__).resolve().parent.parent

PROJECT_ROOT = BASE_DIR.parent / 'myname'

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    ...
    'accounts',
    'blog',
    'core',
]

...

ROOT_URLCONF = 'config.urls'

...

The problem is now, when I run python manage.py makemigrations --settings=config.settings.local I get the error: ModuleNotFoundError: No module named 'accounts'.

So Django seems to not be able to find my apps any more. How can I tell Django what the root directory is where it should look for apps?

I tried using PROJECT_ROOT = BASE_DIR.parent / 'myname' but that didn't help. And I can't find how to set an (pseudo code:) APP_DIR in the Django docs.


Solution

  • Add sys.path.append(os.path.join(BASE_DIR, "myname")) line to your settings.py:

    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 
    sys.path.append(os.path.join(BASE_DIR, "myname"))
    

    Or with pathlib:

    BASE_DIR = Path(__file__).resolve().parent.parent
    sys.path.append(str(BASE_DIR / 'myname'))