Django Cannot import 'main_app' although its already in INSTALLED_APPS

Viewed 222

I can't run my Django app due to the error about the main_app cannot be imported. However, I already have included main_app on the INSTALLED_APPS on settings.py. I have also tested recreating the error and it seems that since my main_app is inside the folder apps, it can't be properly imported. So I tried, moving the main_app to the root folder and subsequently changing apps.main_app to main_app.

Directory

apps
│───main_app
│   │   __init__.py
│   │   asgi.py
│   │   admin.py
│   │   models.py
│   │   tests.py
│   │   urls.py
│   │   views.py
main
│__init__.py
│settings.py
|urls.py
|wsgi.py
│
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'apps.main_app',
]

urls.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('apps.main_app.urls')),
]

main_app urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index-home'),
]

views.py

def index(request):
    return render(request, 'index.html')

Error

file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "D:\Github\SwiftUrl\env\lib\site-packages\django\apps\config.py", line 244, in create
    app_module = import_module(app_name)
  File "c:\users\jerome\appdata\local\programs\python\python39\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'main_app'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\users\jerome\appdata\local\programs\python\python39\lib\threading.py", line 954, in _bootstrap_inner
    self.run()
  File "c:\users\jerome\appdata\local\programs\python\python39\lib\threading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
  File "D:\Github\SwiftUrl\env\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "D:\Github\SwiftUrl\env\lib\site-packages\django\core\management\commands\runserver.py", line 110, in inner_run
    autoreload.raise_last_exception()
  File "D:\Github\SwiftUrl\env\lib\site-packages\django\utils\autoreload.py", line 87, in raise_last_exception
    raise _exception[1]
  File "D:\Github\SwiftUrl\env\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    autoreload.check_errors(django.setup)()
  File "D:\Github\SwiftUrl\env\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "D:\Github\SwiftUrl\env\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "D:\Github\SwiftUrl\env\lib\site-packages\django\apps\registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "D:\Github\SwiftUrl\env\lib\site-packages\django\apps\config.py", line 246, in create
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Cannot import 'main_app'. Check that 'apps.main_app.apps.MainAppConfig.name' is correct.
2 Answers

Clearly from your folder structure, we can see that there is no apps.py file in your main_app. apps.py file shows some configuration regarding the app like its name.

So create an apps.py file in main_app and add the following code;

from django.apps import AppConfig


class MainAppConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'main_app' 

Take a look at the apps.py file inside the main_app folder. Use the same name that is declared in the AppConfig class

Related