ModuleNotFoundError: No module named 'apps' - How to make Django

ghz 8months ago ⋅ 97 views

ModuleNotFoundError: No module named 'apps' - How to make Django find my app?

I'm a Django beginner, also a new SO user, and want to register my app in settings.py.

Software version:

  • Python 3.11.9
  • Django 4.2.11

I created my app (named: Portal) following this guide: startapp with manage.py to create app in another directory

Here's the project tree:

C:.
├───media
│   ├───book-covers
│   └───users
├───static
│   └───admin
│       ├───css
│       ├───fonts
│       └───js
└───project
    ├───apps
    │   ├───auth
    │   ├───portal
    │   │   ├───media
    │   │   ├───migrations
    │   │   │   └───__pycache__
    │   │   ├───static
    │   │   │   └───portal
    │   │   │       ├───css
    │   │   │       ├───icons
    │   │   │       ├───images
    │   │   │       └───js
    │   │   ├───templates
    │   │   │   └───catalog
    │   │   └───__pycache__
    │   └───users
    ├───plugins
    │   └───tinymce
    ├───static
    └───__pycache__

However, when I register my Portal app, Django raised an error:

ModuleNotFoundError: No module named 'apps'

apps.py

from django.apps import AppConfig


class PortalConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'apps.portal'

settings.py

INSTALLED_APPS = [
    '',
    'apps.portal',
    '',
]

I know this might be a duplicate question, but still I can't manage to solve this issue. What should I do to make Django find my app? Please be as specific as possible because I'm totally new. Thanks for your time.

Answers

Based on your project structure, it seems like you have placed your Django app (Portal) within a directory named apps. Therefore, you need to update your INSTALLED_APPS setting in settings.py to correctly reference your app.

Here's what you should do:

  1. Update your INSTALLED_APPS setting in settings.py to correctly reference your app:
INSTALLED_APPS = [
    # Other apps...
    'project.apps.portal',  # Assuming 'project' is the name of your project directory
]
  1. Make sure the apps directory is included in the Python path by adding an empty __init__.py file to it. This file is required to make Python treat the directory as a package.

Your updated project tree should look like this:

C:.
└───project
    ├───__init__.py  # Empty __init__.py file
    ├───apps
    │   ├───auth
    │   ├───portal
    │   │   ├───media
    │   │   ├───migrations
    │   │   ├───static
    │   │   │   └───portal
    │   │   ├───templates
    │   │   └───__pycache__
    │   └───users
    ├───plugins
    │   └───tinymce
    └───static

With these changes, Django should be able to find and register your Portal app correctly. Make sure to restart your Django development server after making these changes to ensure they take effect.