How to fix ModuleNotFoundError in Django?

How to fix ModuleNotFoundError in Django?

How to fix ModuleNotFoundError in Django?

You may have stumbled upon this error:

  File "C:\Python3\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'storages'

and maybe you even tried to fix it but nothing worked? Read this possible causes, there is a solution for each one of them!

Cause : A comma!

Solution: Add comma between list elements!

Lets say you are missing a comma on some line in your installed apps :

INSTALLED_APPS = [
    # [...] Other apps

    # Error cause
    'global'
    'keys'
]

Then code will generate this error:

ModuleNotFoundError: No module named 'globalkeys'

Cause: You didn’t install the app

Solution: Install your extra apps!

Sometimes you just clone a project and forget running

pip install -r > requirements.txt

Sometimes you have Python 2 and Python 3 e.g. on you Linux and pip points to your Python 2 but the Django app runs on Python 3 packages. Then you need to run pip3

pip3 install -r > requirements.txt

Cause: You did install requirements but app is still missing.

Solution: Check if the app is inside your requirements.txt

You may have forgotten to save / commit / add app to your requirements file. Add the missing app to requirements.txt manually and then run:

pip install -r requirements.txt

don’t forget to run:

pip freeze > requirements.txt

afterwards.


Cause: I did all of the above but I’m still getting module missing error.

Solution: Many

  1. You may be installing your module in virtual environment and trying to run app in another terminal without entering venv?
  2. You may have everything working locally but deploy fails e.g. heroku. Then you may have pipenv file that is called Pipfile.lock which is like requirements.txt but your production server may not be set up to trigger installs with it. Then solution is e.g. to install pipenv
sudo apt install pipenv
  1. You may have your Pipfile.lock or requirements.txt commented in .gitignore - can happen to the best! :D

That’s it from me. If you didn’t find your solution post it down and I will try to help you!

Have a good, bug-free day!

Share This:

Don't Miss The Coolest Posts

Subscribe to my newsletter and stay updated.