Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am trying to start up a very basic web app with Django and getting ModuleNotFoundError: No module named 'ui'

My django project is frontend_project and the app is ui

Tree:

front_end
├── front-end/Pipfile
├── front-end/Pipfile.lock
├── front-end/README.md
├── front-end/db.sqlite3
├── front-end/frontend_project
│   ├── front-end/frontend_project/__init__.py
│   ├── front-end/frontend_project/
│   │   └──front-end/frontend_project/ui
│   │       ├── front-end/frontend_project/ui/__init__.py
│   │       ├── front-end/frontend_project/ui/admin.py
│   │       ├── front-end/frontend_project/ui/apps.py
│   │       ├── front-end/frontend_project/ui/migrations
│   │       │   └── front-end/frontend_project/ui/migrations/__init__.py
│   │       ├── front-end/frontend_project/ui/models.py
│   │       ├── front-end/frontend_project/ui/tests.py
│   │       ├── front-end/frontend_project/ui/urls.py
│   │       └── front-end/frontend_project/ui/views.py
│   ├── front-end/frontend_project/settings.py
│   ├── front-end/frontend_project/urls.py
│   └── front-end/frontend_project/wsgi.py
└── front-end/manage.py

Installed_apps:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'ui.apps.UiConfig',

And my frontend_project urls.py looks like:

from django.contrib import admin
from django.urls import path, include
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('ui.urls')),

ui urls.py

from django.urls import path
from .views import homePageView
urlpatterns = [
    path('', homePageView, name='home'),

EDIT: I removed the apps directory and placed the UI app under the project directory - Still experiencing the same error ModuleNotFoundError: No module named 'ui'

I'm just trying to get a simple web app working for right now and my python version is 3.6.7 and pipenv.

Thanks all!

it returns empty, so i guess that it isn't set... should this env variable be set automatically by python? – aphexlog Nov 16, 2018 at 22:34 You probably need to pre-pend "apps" in your INSTALLED_APPS settings. You have everything underneath a directory called "apps" (which isn't standard AFAIK) but that's probably what's going on here. If you brought all those files up a level and deleted "apps" it would probably work just fine. – theWanderer4865 Nov 16, 2018 at 22:47

You have configured your project in an atypical way. While not absolutely required, the most straightforward (and most common) way to structure the project is to include your project apps in the root directory, which is the directory that contains manage.py.

In your case, your 'ui' app directory would be located at front_end/front_end/. (Not under /front_end/front_end/front_end/frontend_project/)

IF you want to keep the project structure that you currently have, there are a additional configuration changes necessary, including subclassing AppConfig (see below).

You will also need to use a dotted path to your application urlconf from your root UrlConf (This is the configuration step that you missed that is likely causing the error).

from django.contrib import admin
from django.urls import path, include
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('frontend_project.ui.urls')), <--- here

In your case, by defining a direct path to 'ui.apps.UiConfig' in INSTALLED_APPS, Django looks for the configuration in the dotted path to the AppConfig subclass for that application which is located in apps.py.

# ui/apps.py
from django.apps import AppConfig
class UiConfig(AppConfig):
    name = 'ui'
                If i have done what? 'ui.apps.UiConfig' is in my settings already and my apps.pt already looks exactly like that, so what are you suggesting hat i do?
– aphexlog
                Nov 19, 2018 at 16:00
                Also, I reverted back to the way I had the directory structure in the past frontend_project/ui/ and having the same error: ModuleNotFoundError: No module named 'ui'
– aphexlog
                Nov 19, 2018 at 16:00
                move it up to dir with manage.py --> front_end/front_end  so that dir would include: frontend_project/, ui/, README.md, manage.py, db.sql..., Pip..., Pipfile.lock; frontend_project/ would include settings.py, urls.py, wsgi.py, init.py; ui/ would include admin.py, apps.py, models.py, tests.py, urls.py, views.py, init.py, migrations/
– chuck_sum
                Nov 19, 2018 at 23:18
                I had the ecact same issue what my directory structure was front-end/frontend_project/ui ... I reverted it back to that and i'm having the same issue.
– aphexlog
                Nov 19, 2018 at 15:53

Import urls.py file of Ui app in frontend_project urls.py file.

from django.contrib import admin
from django.urls import path, include
from ui import urls
urlpatterns = [
      path('admin/', admin.site.urls),
      path('', include('ui.urls')),
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.