ModuleNotFoundError: No module named ‘django’ – Common Django Errors

If you got the error “ModuleNotFoundError: No module named ‘django'” it means that Python couldn’t find your Django package. Here’s the full error message:

Traceback (most recent call last):
  File "manage.py", line 10, in main
    from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 16, in main
    ) from exc
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?

If you know that Django was installed, here are some quick things to check:

  • If you installed it in a virtual environment, make sure that you activate your virtual environment:
$ source venv/bin/activate
  • To check which paths Python looks in for the django module you can run this code:
(venv) $ python
Python 3.6.9 (default, Nov  7 2019, 10:44:02)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.path)
['', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/opt/code/venv/lib/python3.6/site-packages']

If Django is not located in one of those paths, it means that you didn’t install Django or didn’t install it in the right location.

If you didn’t install the Django package lets do that now. First, install and activate your virtual environment. Then install Django with this command:

(venv) $ pip install django

Now you can rerun manage.py and everything should work

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top