Errors while coding makes our head go around, of which ModuleNotFoundError is one of them, and we will be learning how to remove this error in the case of the dotenv module. ModuleNotFoundError: No module named dotenv happens when we try to import the ‘python-dotenv’ module before installing it on our computer.
How to install ‘python-dotenv’:-
We can install it in various ways:-
For python3, install it using the following command:-
pip3 install python-dotenv
If pip is not in the PATH environment variable, then use:-
python3 -m pip install python-dotenv
For Anaconda, we can install it using:-
conda install -c forge python-dotenv
For pycharm:-
We first need to install python-dotenv using pip and then add the package to pycharm by doing File > Settings > Project > Python Interpreter > + > python-dotenv > Install Package.
To have dotenv present in our docker container, we need to include it in our Dockerfile and then build our Dockerfile to create new image, after which we should be able to use those packages in our code. To use dotenv we should use:-
RUN pip install python-dotenv //IN Windows
RUN apt-get install python-dotenv//in Ubuntu
For using dotenv in Django project, make sure that it is installed in your system, using commands mentioned above and python-dotenv = “Version_name”, (where Version_name corresponds to the version of the python-dotenv installed), is present in the Pipfile.
What are Environment Variables?
We use environment variables to set some values to particular variables, and can later change those values to change the way our process would behave. We can even hard code those variables in our code, but we simply keep them out of our code, so that we can easily change the way our process would behave.
Hard coding those variables in our code may also leak some sensitive information. We create a ‘.env’ file to store our environment variables and we can use the dotenv module to access those variables stored in the ‘.env’ File. The ‘.env’ File needs to be in our project’s root directory so that it becomes accessible to our module.
The below code shows how we can use dotenv in our code so as to access the variables stored in ‘.env’ File:-
import os
from dotenv import load_dotenv
load_dotenv()
print(os.environ.get('NAME'))
print(os.environ.get('Age'))
The output of the above program will be :-
The ‘.env’ File contains the key – value pair, storing the value of the variables which we will be using, as in the below image we have used NAME and AGe variables and given their value and have accessed them in our code:-
NAME = "Rahul"
Age = 21
The various reasons why “ModuleNotFoundError : No module named dotenv” occurs:-
- We may not have the ‘python-dotenv’ package installed in our system prior to using its functions in our code.
- We may have installed a different version of the dotenv module than the one we are using.
- dotenv package may not be installed in the virtual environment.
- We may have named our module ‘dotenv.py’, which will override the original dotenv module.
- We may have named a variable as ‘dotenv,’ which will override it. To solve it, we should not name any variable as ‘dotenv.’
What to do if a different version of dotenv is installed or dotenv is not present in our system:-
We must install the dotenv module for the correct version of python, which we are currently using.
For example:-
Firstly we should check the python version and then install dotenv accordingly:- For python2, we should use:-
pip install python-dotenv
For python3, we need to use:-
pip3 install python-dotenv
If ‘pip’ is not in the PATH environment variable, then we must replace ‘pip’ with ‘python3 -m pip’ while installing the dotenv module:-
python3 -m pip install python-dotenv
After the installation, close the IDE and restart it, to see whether it is working or not.
We can check whether python-dotenv is installed or not by the following command:-
pip show python-dotenv
OR
pip3 show python-dotenv
depending upon the python version.
The output of the above command should be that the package is not installed or if it has been installed, then it must show some details like the name of the module, python version, its summary, author, GitHub repo link, license, location on the system, etc. As in the below diagram:-
If the problem persists, then we should correct the version of Python our IDLE is using, as the python version in our IDLE or virtual environment must correspond to the version of python-dotenv.
What to do if python-dotenv is not installed for our virtual environment:-
While using a virtual environment, we must ensure that it has ‘python-dotenv’ installed.
Create a virtual environment:-
python3 -m venv venv
To activate in Unix or Mac, write the following code:-
source venv/bin/activate
To activate in Windows from the command line, write:-
venv\Scripts\activate.bat
After that, install python-dotenv in it:-
pip install python-dotenv
What if we have declared a variable as dotenv:-
We must not declare a variable named ‘dotenv,’ as it will override the original dotenv module.
What if our module name is dotenv.py:-
We must not name our module dotenv.py as it will shadow the original dotenv, which we are supposed to use in our code.
If, even after this much troubleshooting, the problem is not resolved, then we can try uninstalling the ‘dotenv’ module and then reinstalling it again.
We can also upgrade our package to see if that would work. We can upgrade it using the following command:-
pip3 install python-dotenv --upgrade
FAQs
The “ModuleNotFoundError” is raised when Python is not able to locate the module which we are trying to import, it can occur when we either have forgotten to install the module, or we would have installed it incorrectly.
Environment variables in a separate ‘.env’ file help to keep our code clean and keep it safe from malicious access to the people who can misuse its access. We may even encrypt our environment variables. We can keep API keys to keep them secure.
Why should we use a virtual environment for our projects?
A virtual environment should be used for our project so that we should not break the compatibility for our earlier projects by upgrading the python version to make it compatible with the recent one.
Conclusion
These are the methods that we can apply to solve the “ModuleNotFoundError: No module named ‘dotenv’ ” error if it occurs with us in our code.