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', ]
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
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 🧡
Top comments (1)
how do I work the applications within a directory at the same level of the django project?
To maintain an order, I put the applications inside a folder called 'Apps' in the root directory
from django.apps import AppConfig
default_auto_field = 'django.db.models.BigAutoField'
name = 'Apps.Aplicacion1'
INSTALLED_APPS = [
'ModuleNotFoundError: No module named 'Aplicacion1'
How to fix it?