ModuleNotFoundError: no module named environ (django-environ package)

Ye ol’ mystery error

Django keeps telling me that there is no module called “environ” when I try to run python manage.py runserver or python manage.py check (I’ll paste the full Traceback below). The environ module is something I import in settings.py; it belongs to the django-environ package, which I use to keep a lot of settings.py related variables in a .env file (secret keys, DB name and password, etc.).

What I’ve checked

I’m pretty dumbfounded because I’ve checked my virtual environment a bunch of times, and the django-environ package is definitely installed. I’ve also manually gone in and checked the virtual environment for the module, and it is right there (venv/lib/environ/environ.py).

Potential causes

It’s got to do with with one of two things that I changed recently (because this local version of my project was working yesterday on my Mac):

  1. I completely uninstalled homebrew so that I could reinstall it and upgrade Python from 3.8.x to 3.10.14.
  2. I created a whole new virtual environment today and upgraded a bunch of stuff (primarily upgraded to Django 5.0.6 (was version 4.2.3). I’ll put full list of upgraded libraries below.

Thanks in advance

Any help would be greatly appreciated. I realize that nobody probably wants to debug dumb stuff like this.

All the libraries I upgraded

requirements.txt was:

aiohttp==3.9.3
aiosignal==1.3.1
asgiref==3.6.0
async-timeout==4.0.3
attrs==23.2.0
backports.zoneinfo==0.2.1
certifi==2023.5.7
charset-normalizer==3.1.0
Django==4.2.3
django-debug-toolbar==4.1.0
django-environ==0.9.0
django-extensions==3.2.1
django-sendgrid-v5==1.2.2
frozenlist==1.4.1
gunicorn==21.2.0
idna==3.4
multidict==6.0.5
packaging==23.1
pkg_resources==0.0.0
psycopg==3.1.9
psycopg-binary==3.1.9
python-http-client==3.3.7
PyVimeo==1.1.0
requests==2.31.0
sendgrid==6.10.0
six==1.16.0
sqlparse==0.4.3
starkbank-ecdsa==2.2.0
stripe==5.4.0
tinydb==4.8.0
tuspy==1.0.3
typing_extensions==4.7.1
urllib3==2.0.2
yarl==1.9.4

Now it is:

aiohttp==3.9.5
aiosignal==1.3.1
asgiref==3.8.1
async-timeout==4.0.3
attrs==23.2.0
certifi==2024.2.2
charset-normalizer==3.3.2
Django==5.0.6
django-debug-toolbar==4.4.2
django-environ==0.11.2
django-extensions==3.2.3
django-sendgrid-v5==1.2.3
frozenlist==1.4.1
gunicorn==22.0.0
idna==3.7
multidict==6.0.5
packaging==24.0
psycopg==3.1.19
psycopg-binary==3.1.19
python-http-client==3.3.7
PyVimeo==1.1.2
requests==2.32.3
sendgrid==6.11.0
six==1.16.0
sqlparse==0.5.0
starkbank-ecdsa==2.2.0
stripe==9.9.0
tinydb==4.8.0
tuspy==1.0.3
typing_extensions==4.12.0
urllib3==2.2.1
yarl==1.9.4

Here is the full error Traceback:

python manage.py runserver
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/django/core/management/base.py", line 413, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.10/site-packages/django/core/management/commands/runserver.py", line 74, in execute
    super().execute(*args, **options)
  File "/usr/local/lib/python3.10/site-packages/django/core/management/base.py", line 459, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.10/site-packages/django/core/management/commands/runserver.py", line 81, in handle
    if not settings.DEBUG and not settings.ALLOWED_HOSTS:
  File "/usr/local/lib/python3.10/site-packages/django/conf/__init__.py", line 89, in __getattr__
    self._setup(name)
  File "/usr/local/lib/python3.10/site-packages/django/conf/__init__.py", line 76, in _setup
    self._wrapped = Settings(settings_module)
  File "/usr/local/lib/python3.10/site-packages/django/conf/__init__.py", line 190, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/local/Cellar/python@3.10/3.10.14/Frameworks/Python.framework/Versions/3.10/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/Users/TRON/Documents/SCIENCE/CMPTRS/HTML/webProjectsForPublishing/terp/terpBackend/djangoTerp/djangoTerp/settings.py", line 14, in <module>
    import environ
ModuleNotFoundError: No module named 'environ'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/TRON/Documents/SCIENCE/CMPTRS/HTML/webProjectsForPublishing/terp/terpBackend/djangoTerp/manage.py", line 22, in <module>
    main()
  File "/Users/TRON/Documents/SCIENCE/CMPTRS/HTML/webProjectsForPublishing/terp/terpBackend/djangoTerp/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.10/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.10/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.10/site-packages/django/core/management/base.py", line 426, in run_from_argv
    connections.close_all()
  File "/usr/local/lib/python3.10/site-packages/django/utils/connection.py", line 84, in close_all
    for conn in self.all(initialized_only=True):
  File "/usr/local/lib/python3.10/site-packages/django/utils/connection.py", line 76, in all
    return [
  File "/usr/local/lib/python3.10/site-packages/django/utils/connection.py", line 73, in __iter__
    return iter(self.settings)
  File "/usr/local/lib/python3.10/site-packages/django/utils/functional.py", line 47, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/local/lib/python3.10/site-packages/django/utils/connection.py", line 45, in settings
    self._settings = self.configure_settings(self._settings)
  File "/usr/local/lib/python3.10/site-packages/django/db/utils.py", line 148, in configure_settings
    databases = super().configure_settings(databases)
  File "/usr/local/lib/python3.10/site-packages/django/utils/connection.py", line 50, in configure_settings
    settings = getattr(django_settings, self.settings_name)
  File "/usr/local/lib/python3.10/site-packages/django/conf/__init__.py", line 89, in __getattr__
    self._setup(name)
  File "/usr/local/lib/python3.10/site-packages/django/conf/__init__.py", line 76, in _setup
    self._wrapped = Settings(settings_module)
  File "/usr/local/lib/python3.10/site-packages/django/conf/__init__.py", line 190, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/local/Cellar/python@3.10/3.10.14/Frameworks/Python.framework/Versions/3.10/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/Users/TRON/Documents/SCIENCE/CMPTRS/HTML/webProjectsForPublishing/terp/terpBackend/djangoTerp/djangoTerp/settings.py", line 14, in <module>
    import environ
ModuleNotFoundError: No module named 'environ'

Solution

Turns out my python command was aliased to the OS instead of my venv. :man_facepalming:

  • I deleted the old venv
  • recreated another venv using
/usr/local/opt/python@3.10/bin/python3.10 -m venv ./venv
  • reinstalled all the package dependencies from my requirements.txt file (all the updated dependencies, including Django 5.0.6)

and now as long as I use python3 when calling python3 manage.py runserver, it works fine (which python still returns an alias to my OS installation of python while which python3 returns the installation in my venv).

Given the previous paragraph :point_up_2:, I probably didn’t need to delete and recreate my venv, but oh well :man_shrugging: