DEV Community

Cover image for Two most common cause of "Django Import Error: No module named <appname>" in Django.
Gaurav
Gaurav

Posted on

Two most common cause of "Django Import Error: No module named <appname>" in Django.

Every django developer has experience of facing this issue. It is one of the most common errors you will face when running your first migration. This error can also produce other errors such as "TemplateDoesNotExist".

In this article we will see two most common reasons that cause Django Import Error: No module named

1. Forgot to put app name to installed_apps list?

The most common mistake that beginners make is that they forget to list their apps in INSTALLED_APPS which is present in the settings.py file.

Therefore, please make sure that your app is listed in the INSTALLED_APPS. Example:
Suppose, I have a newly started app called users within a project named polls project then I would navigate to pollsproject/settings.py and will search for INSTALLED_APPS and will append 'users' or 'users.apps.UsersConfig' to it.

INSTALLED_APPS = [
   'users.apps.UsersConfig', # added users app here
   'django.contrib.admin',
   'django.contrib.auth',
   'django.contrib.contenttypes',
   'django.contrib.sessions',
   'django.contrib.messages',
   'django.contrib.staticfiles',
]
Enter fullscreen mode Exit fullscreen mode

This is the most common solution for TemplateDoesNotExist error as well so make sure you have not committed this mistake.

Now, let's look at another most common mistake that a beginner makes which results in this error.

2. App is not in the root directory of your project.

If you have listed your apps inside INSTALLED_APPS but still you are facing the *Django Import Error: No module named * error then it might be due to django could not find your app within the root directory.

Your apps can be place anywhere, but Django by default starts to search app from the project's root directory and if it does not find the app there then it will throw the same ImportError error.

Therefore, please make sure that your app is located in the same directory where manage.py file is located. For example, for a random project named pollsproject, I have created an app called users, then the whole tree structure of my project would look like:

polls-project/
├── env/
├── pollsproject/
│   ├── pollsproject/
│   │   ├── __init__.py
│   │   ├── asgi.py
│   │   ├── settings.py
│   │   ├── urls.py
│   │   └── wsgi.py
│   ├── users/
│   │   ├── migrations/
│   │   ├── __init__.py
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── models.py
│   │   ├── tests.py
│   │   └── views.py
│   ├── db.sqlite3
│   └── manage.py
├── .gitignore
├── requirements.txt
Enter fullscreen mode Exit fullscreen mode

The main cause of this problem is that when you start a fresh project and then you start an app using the django-admin command from the same location from where you run the startproject command. This will create an app outside the project's root directory. Therefore, to avoid this issue use python manage.py startapp instead of "django-admin startapp or make sure you’re in the same directory as manage.py

The good news is that Django's apps are "Pluggable" therefore, you can just move your app to the directory same as manage.py and it should work.

Let me know if none of these two worked for you, I will be more than glad to help you.

Happy Coding 🧡

Oldest comments (1)

Collapse
 
imyourpartner profile image
Yes, I'm you partner • Edited

how do I work the applications within a directory at the same level of the django project?

example
/
/db.sqlite3
/venv/
/DjangoProject/
/manage.py
/Apps/
/Apps/app1/
/Apps/app2/
/Apps/app3/
/Apps/appN../

To maintain an order, I put the applications inside a folder called 'Apps' in the root directory

C:\siw\Apps\Aplicacion1\apps.py
from django.apps import AppConfig

class AplicacionConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'Apps.Aplicacion1'

C:\siw\proyecto\settings.py
INSTALLED_APPS = [
...
'Apps.Aplicacion1'

]

ERROR:
'ModuleNotFoundError: No module named 'Aplicacion1'

How to fix it?