ModuleNotFoundError: No module named 'main'

Hey i have been working on this project for a while but i ran into a problem and i am currently stuck…

I have already looked at all the possible solutions that other people have used to eliminate this problem, but unfortunately I have not been able to solve my problem.

This is what my apps/views.py looks like:

from django.views.generic import TemplateView

class AppsView(TemplateView):
    pass

# DTF
apps_dtf_view = AppsView.as_view(
    template_name="apps/apps-apps-dtf.html"
)

# LAGER
apps_lager_anlieferung_dashboard_view = AppsView.as_view(
    template_name="apps/lager/anlieferung-dashboard.html"
)
apps_lager_anlieferung_annahme_view = AppsView.as_view(
    template_name="apps/lager/anlieferung-annahme.html"
)
apps_lager_freier_wareneingang_view = AppsView.as_view(
    template_name="apps/lager/freier-wareneingang.html"
)
apps_lager_freier_wareneingang_dashboard_view = AppsView.as_view(
    template_name="apps/lager/freier-wareneingang-dashboard.html"
)
apps_lager_freier_wareneingang_ausbuchen_view = AppsView.as_view(
    template_name="apps/lager/freier-wareneingang-ausbuchen.html"
)

# PICKPACK
apps_pickpack_box_buchen_view = AppsView.as_view(
    template_name="apps/pickpack/box-buchen.html"
)
apps_pickpack_item_in_box_view = AppsView.as_view(
    template_name="apps/pickpack/item-in-box.html"
)
apps_pickpack_sammler_view = AppsView.as_view(
    template_name="apps/pickpack/sammler.html"
)
apps_pickpack_sammler_versendete_view = AppsView.as_view(
    template_name="apps/pickpack/sammler-versendete.html"
)
apps_pickpack_einzeljob_versandfertig_view = AppsView.as_view(
    template_name="apps/pickpack/einzeljob_versandfertig.html"
)
apps_pickpack_status_resetten_view = AppsView.as_view(
    template_name="apps/pickpack/status-resetten.html"
)
apps_pickpack_statistiken_view = AppsView.as_view(
    template_name="apps/pickpack/statistiken.html"
)
apps_pickpack_regal_uebersicht_view = AppsView.as_view(
    template_name="apps/pickpack/regal-uebersicht.html"
)

# VERSAND
apps_versand_view = AppsView.as_view(
    template_name="apps/apps-apps-versand.html"
)

# QUALI
apps_quali_substep_fertigstellen_view = AppsView.as_view(
    template_name="apps/quali/substep-fertigstellen.html"
)
apps_quali_item_try_erhoehen_view = AppsView.as_view(
    template_name="apps/quali/item-try-erhoehen.html"
)

my apps/urls.py:

from django.urls import path

from .views import (
    apps_dtf_view,
    apps_lager_anlieferung_dashboard_view,
    apps_lager_anlieferung_annahme_view,
    apps_lager_freier_wareneingang_view,
    apps_lager_freier_wareneingang_dashboard_view,
    apps_lager_freier_wareneingang_ausbuchen_view,
    apps_pickpack_box_buchen_view,
    apps_pickpack_item_in_box_view,
    apps_pickpack_sammler_view,
    apps_pickpack_sammler_versendete_view,
    apps_pickpack_einzeljob_versandfertig_view,
    apps_pickpack_status_resetten_view,
    apps_pickpack_statistiken_view,
    apps_pickpack_regal_uebersicht_view,
    apps_versand_view,
    apps_quali_substep_fertigstellen_view,
    apps_quali_item_try_erhoehen_view,

)

app_name = "apps"

urlpatterns = [
    # DTF
    path(
        "dtf",
        view=apps_dtf_view,
        name="dtf"
    ),
    # LAGER
    path(
        "lager/anlieferung-dashboard",
        view=apps_lager_anlieferung_dashboard_view,
        name="lager.anlieferung_dashboard"
    ),
    path(
        "lager/anlieferung-annahme",
        view=apps_lager_anlieferung_annahme_view,
        name="lager.anlieferung_annahme"
    ),
    path(
        "lager/freier-wareneingang",
        view=apps_lager_freier_wareneingang_view,
        name="lager.freier_wareneingang"
    ),
    path(
        "lager/freier-wareneingang-dashboard",
        view=apps_lager_freier_wareneingang_dashboard_view,
        name="lager.freier_wareneingang_dashboard"
    ),
    path(
        "lager/freier-wareneingang-ausbuchen",
        view=apps_lager_freier_wareneingang_ausbuchen_view,
        name="lager.freier_wareneingang_ausbuchen"
    ),
    # PICKPACK
    path(
        "pickpack/box-buchen",
        view=apps_pickpack_box_buchen_view,
        name="pickpack.box_buchenn"
    ),
    path(
        "pickpack/item-in-box",
        view=apps_pickpack_item_in_box_view,
        name="pickpack.item_in_box"
    ),
    path(
        "pickpack/sammler",
        view=apps_pickpack_sammler_view,
        name="pickpack.sammler"
    ),
    path(
        "pickpack/sammler-versendete",
        view=apps_pickpack_sammler_versendete_view,
        name="pickpack.sammler_versendete"
    ),
    path(
        "pickpack/einzeljob-versandfertig",
        view=apps_pickpack_einzeljob_versandfertig_view,
        name="pickpack.einzeljob_versandfertig"
    ),
    path(
        "pickpack/status-resetten",
        view=apps_pickpack_status_resetten_view,
        name="pickpack.status_resetten"
    ),
    path(
        "pickpack/statistiken",
        view=apps_pickpack_statistiken_view,
        name="pickpack.statistiken"
    ),
    path(
        "pickpack/regal-uebersicht",
        view=apps_pickpack_regal_uebersicht_view,
        name="pickpack.regal_uebersicht"
    ),
    # VERSAND
    path(
        "versand",
        view=apps_versand_view,
        name="varsand"
    ),
    # QUALI
    path(
        "quali/substep-fertigstellen",
        view=apps_quali_substep_fertigstellen_view,
        name="quali.substep_fertigstellen_view"
    ),
    path(
        "quali/item-try-erhoehen",
        view=apps_quali_item_try_erhoehen_view,
        name="quali.item_try_erhoehen"
    ),
]

main/settings.py:

"""
Django settings for main project.

Generated by 'django-admin startproject' using Django 4.1.5.

For more information on this file, see
https://docs.djangoproject.com/en/4.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.1/ref/settings/
"""
import os
from pathlib import Path

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


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-4+h=x+oz8d=d$6f%3mwenx&z0p8r^4piz_c^d7g-4ue-8@1pcb'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['*']


# Application definition

DEFAULT_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'main'
]

LOCAL_APPS = [
    "apps",
    # "system",
    # "t_z",
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

INSTALLED_APPS = DEFAULT_APPS + LOCAL_APPS

ROOT_URLCONF = 'main.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, "templates")],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'main.wsgi.application'


# Database
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/4.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/

STATIC_URL = 'static/'

# Default primary key field type
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

my main/urls.py:

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

urlpatterns = [
    path('admin/', admin.site.urls),

    path("", views.DashboardView.as_view(), name="dashboard"),

    # APPS
    path("apps/", include("apps.urls")),
    # # SYSTEM
    # path("system/", include("system.urls")),
    # # T/Z
    # path("t_z/", include("t_z.urls")),
]

main/views.py (just for my Dashboard):

from django.shortcuts import render
from django.views import View
from django.contrib.auth.mixins import LoginRequiredMixin

# Dashboard
class DashboardView(LoginRequiredMixin, View):
    def get(self, request):
        return render(request, "dashboard.html")

main/wsgi.py:

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'main.settings')

application = get_wsgi_application()

and main/asgi.py:

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'main.settings')

application = get_asgi_application()

I assume that the problem is here, but if you need anything else, please write

If anyone knows/sees the problem, I’d appreciate an explanation and solution to my problem, thanks in advance!

What specifically is throwing this error?

Please post the complete traceback.

Hey Ken, thank you for such a quick reply

This is what a complete error looks like:

 python manage.py runserver
Traceback (most recent call last):
  File "C:\Users\shirtee_b.nikolic\Documents\Documents\IT Ausbildung\projects\bnb\phio\Admin\env\lib\site-packages\django\core\management\base.py", line 402, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\shirtee_b.nikolic\Documents\Documents\IT Ausbildung\projects\bnb\phio\Admin\env\lib\site-packages\django\core\management\commands\runserver.py", line 74, in execute     
    super().execute(*args, **options)
  File "C:\Users\shirtee_b.nikolic\Documents\Documents\IT Ausbildung\projects\bnb\phio\Admin\env\lib\site-packages\django\core\management\base.py", line 448, in execute
    output = self.handle(*args, **options)
  File "C:\Users\shirtee_b.nikolic\Documents\Documents\IT Ausbildung\projects\bnb\phio\Admin\env\lib\site-packages\django\core\management\commands\runserver.py", line 81, in handle      
    if not settings.DEBUG and not settings.ALLOWED_HOSTS:
  File "C:\Users\shirtee_b.nikolic\Documents\Documents\IT Ausbildung\projects\bnb\phio\Admin\env\lib\site-packages\django\conf\__init__.py", line 92, in __getattr__
    self._setup(name)
  File "C:\Users\shirtee_b.nikolic\Documents\Documents\IT Ausbildung\projects\bnb\phio\Admin\env\lib\site-packages\django\conf\__init__.py", line 79, in _setup
    self._wrapped = Settings(settings_module)
  File "C:\Users\shirtee_b.nikolic\Documents\Documents\IT Ausbildung\projects\bnb\phio\Admin\env\lib\site-packages\django\conf\__init__.py", line 190, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "C:\Users\shirtee_b.nikolic\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 992, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'main'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\shirtee_b.nikolic\Documents\Documents\IT Ausbildung\projects\bnb\phio\manage.py", line 22, in <module>
    main()
  File "C:\Users\shirtee_b.nikolic\Documents\Documents\IT Ausbildung\projects\bnb\phio\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\shirtee_b.nikolic\Documents\Documents\IT Ausbildung\projects\bnb\phio\Admin\env\lib\site-packages\django\core\management\__init__.py", line 446, in execute_from_command_
line
    utility.execute()
  File "C:\Users\shirtee_b.nikolic\Documents\Documents\IT Ausbildung\projects\bnb\phio\Admin\env\lib\site-packages\django\core\management\__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\shirtee_b.nikolic\Documents\Documents\IT Ausbildung\projects\bnb\phio\Admin\env\lib\site-packages\django\core\management\base.py", line 415, in run_from_argv
    connections.close_all()
  File "C:\Users\shirtee_b.nikolic\Documents\Documents\IT Ausbildung\projects\bnb\phio\Admin\env\lib\site-packages\django\utils\connection.py", line 84, in close_all
    for conn in self.all(initialized_only=True):
  File "C:\Users\shirtee_b.nikolic\Documents\Documents\IT Ausbildung\projects\bnb\phio\Admin\env\lib\site-packages\django\utils\connection.py", line 76, in all
    return [
  File "C:\Users\shirtee_b.nikolic\Documents\Documents\IT Ausbildung\projects\bnb\phio\Admin\env\lib\site-packages\django\utils\connection.py", line 73, in __iter__
    return iter(self.settings)
  File "C:\Users\shirtee_b.nikolic\Documents\Documents\IT Ausbildung\projects\bnb\phio\Admin\env\lib\site-packages\django\utils\functional.py", line 57, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\shirtee_b.nikolic\Documents\Documents\IT Ausbildung\projects\bnb\phio\Admin\env\lib\site-packages\django\utils\connection.py", line 45, in settings
    self._settings = self.configure_settings(self._settings)
  File "C:\Users\shirtee_b.nikolic\Documents\Documents\IT Ausbildung\projects\bnb\phio\Admin\env\lib\site-packages\django\db\utils.py", line 148, in configure_settings
    databases = super().configure_settings(databases)
  File "C:\Users\shirtee_b.nikolic\Documents\Documents\IT Ausbildung\projects\bnb\phio\Admin\env\lib\site-packages\django\utils\connection.py", line 50, in configure_settings
    settings = getattr(django_settings, self.settings_name)
  File "C:\Users\shirtee_b.nikolic\Documents\Documents\IT Ausbildung\projects\bnb\phio\Admin\env\lib\site-packages\django\conf\__init__.py", line 92, in __getattr__
    self._setup(name)
  File "C:\Users\shirtee_b.nikolic\Documents\Documents\IT Ausbildung\projects\bnb\phio\Admin\env\lib\site-packages\django\conf\__init__.py", line 79, in _setup
    self._wrapped = Settings(settings_module)
  File "C:\Users\shirtee_b.nikolic\Documents\Documents\IT Ausbildung\projects\bnb\phio\Admin\env\lib\site-packages\django\conf\__init__.py", line 190, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "C:\Users\shirtee_b.nikolic\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 992, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'main'

In your main/urls.py file, you have the line import views.

If you’re looking to import from the main app, it would be from main import views, or possibly import .views.

In my main/urls.py i just want to include apps/views.py, so i tried that with:

from django.contrib import admin
from django.urls import path, include
from Admin.apps import views

urlpatterns = [
   path('admin/', admin.site.urls),

   path("", views.DashboardView.as_view(), name="dashboard"),

   # APPS
   path("apps/", include("apps.urls")),

but the error is still there

What directory is the views file you want to import in.

with import views i’m importing main/views.py for my Dashboard View and I don’t think that’s a problem, however i need to include my directory “apps” somehow

If you’re trying to import two modules named “views” from two different directories, you need to change the name of one of them using the as clause.
e.g. from apps import views as apps_views giving you a distinct name you can use in that file.

doesn’t this finish my job:

path("apps/", include("apps.urls"))

because i actually don’t need to import anything from my apps directory

That is entirely beside the point.

In your urls, you have:

That name, views, needs to be imported from somewhere.

I don’t know what file you’re trying to reference at that point. And I have not yet seen a valid import statement for that in what you’ve posted.

Using ‘import views’ im importing ‘main/views.py’ (because its the same directory) and with ‘path(“”, views.DashboardView.as_view(), name=“dashboard”),’ im just using it.

And even if i delete it, im still getting the same error

Answered above at ModuleNotFoundError: No module named 'main' - #4 by KenWhitesell