ModuleNotFoundError: No module named 'pytz'

I am trying to test my Django web app project using Docker. I run docker build -t backend:latest --build-arg FRONTEND_URL=localhost:3000 --build-arg BACKEND_URL=localhost:8000 . in powershell first. Then I run docker run -e "PORT=8000" -p 8000:8000 backend
It gives me the error

Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/threading.py", line 973, in _bootstrap_inner
    self.run()
  File "/usr/local/lib/python3.9/threading.py", line 910, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 115, in inner_run
    autoreload.raise_last_exception()
  File "/usr/local/lib/python3.9/site-packages/django/utils/autoreload.py", line 87, in raise_last_exception
    raise _exception[1]
  File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 381, in execute
    autoreload.check_errors(django.setup)()
  File "/usr/local/lib/python3.9/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python3.9/site-packages/django/apps/registry.py", line 114, in populate
    app_config.import_models()
  File "/usr/local/lib/python3.9/site-packages/django/apps/config.py", line 300, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/src/meetings/models.py", line 5, in <module>
    from pytz import timezone
ModuleNotFoundError: No module named 'pytz'

But I have pytz installed using pip install pytz so I don’t knwo what happened.

Without being a python developer I would say that django is what cannot find that module. Since I don’t know what is inside the Dockerfile I don’t know what could cause it. Can you share at least a part of your Dockerfile? The part where you install the module and maybe django. Of course the best would be to see the whole file but I don’t want to ask that if it is a private project. However I don’t think we can tell what the problem is without further details. Maybe python developers who use django could guess it based on their experience.

1 Like

I ran into this error as well, and noticed what @rimelek said about Django not being able to find pytz, so I added this line to my requirements.txt file which fixed this issue for me:
pytz==2021.3
Seeing how Django 4 just came out, that might be related, but this combination of requirements seems to work for me:

Django==4.0
pytz==2021.3

To make it short, it means that you lacked some “dependencies” for the libraries you wanted to use. This is a common problem when installing python packages, mainly in windows. Before trying to use any kind of library, first it is suggested to look up whether it needs another library in python “family”.

The solution is to provide the python interpreter with the path-to-your-module/library. The simplest solution is to append that python path to your sys.path list. In your notebook, first try:

import sys
sys.path.append('my/path/to/module/folder')

This isn’t a permanent change in sys.path, because when you log out, your environment is reset, so any variables you may have set are lost.

The better (and more permanent) way to solve this is to set your PYTHONPATH, which provides the interpreter with additional directories look in for python packages/modules.

from BASH type: export PYTHONPATH=/path/to/new/folder:/another/path/...../

#each path must be separated by a colon