ModuleNotFoundError: No module named 'environ' in Python

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

ModuleNotFoundError: No module named 'environ' in Python #

The Python "ModuleNotFoundError: No module named 'environ'" occurs when we forget to install the django-environ module before importing it or install it in an incorrect environment. To solve the error, install the module by running the pip install django-environ command.

no module named environ

Open your terminal in your project's root directory and install the django-environ module.

shell
# 👇️ make sure to run it with VENV activated pip install django-environ # 👇️ if you don't have pip in your PATH environment variable python -m pip install django-environ # 👇️ for Anaconda conda install -c conda-forge django-environ
Make sure to activate your virtual environment before running the command.

If you don't have a virtual environment, create one with the following commands.

shell
# 👇️ use correct version of Python when creating VENV python3 -m venv venv # 👇️ activate on Unix or MacOS source venv/bin/activate # 👇️ activate on Windows (cmd.exe) venv\Scripts\activate.bat # 👇️ activate on Windows (PowerShell) venv\Scripts\Activate.ps1 # 👇️ install django-environ in virtual environment pip install django-environ # 👇️ install the modules in your requirements.txt file pip install -r requirements.txt

Your virtual environment will use the version of Python that was used to create it.

With your virtual environment activated, make sure to install all modules by passing your requirements.txt file to pip.

shell
# 👇️ install the modules in your requirements.txt file pip install -r requirements.txt

Now you should be able to use the django-environ module in your settings.py file.

Make sure you have a .env file in the root directory of your project. Your .env file should look something like:

.env
DEBUG=on SECRET_KEY=your-secret-key

For examples of using the module, refer to the official documentation.

Conclusion #

The Python "ModuleNotFoundError: No module named 'environ'" occurs when we forget to install the django-environ module before importing it or install it in an incorrect environment. To solve the error, install the module by running the pip install django-environ command.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.