I have installed python-dotenv but python cannot find it

Question:

i am using dotenv in a flask project, and have also tested this in a dumbed down test environment as well. I have tried uninstalling and reinstalling etc but the dotenv module cannot be found by python.

When starting flask it sees there are some .env files and tells me to install dotenv even though it is installed and i can see it in flasks system libraries.

This is what happens at the command line.

When I run code I get a module not found error saying it can’t find dotenv. The code is

import os
from dotenv import load_dotenv
from flask import Flask
from flask_sqlalchemy import SQLAlchemy

and the error is

The error message

Any advice gratefully accepted.

Asked By: Mark Kortink

||

Answers:

It’s possible that you also have the “dotenv” package installed.

In your virtual environment, try:

pip uninstall dotenv
pip uninstall python-dotenv
pip install python-dotenv

Also you may have dotenv installed at the system level (outside of your virtual environment). If yes, you could try uninstalling that.

If this is not the issue then please post your code and the resultant error.

Answered By: Greg Cowell

It turns out there were a number of problems with my code and I will briefly list them here in case anyone else experiences the same problems.

1st Problem

Being reasonably new I am not really clear how the python ecosystem I have installed all hangs together. I have installed Anaconda and Spyder as my development environment. However I have been following a Flask tutorial that uses pip as the installer with virtual environments. The command prompt I use is the one that came with Anaconda. Everything seemed to be working OK somehow, until I got the dotenv problem, which is in fact a tiny detail in the over-all rather large tutorial.

To fix dotenv I was trying all sorts of install/uninstalls with pip, I could see dotenv was installed already! That didn’t work. What did work was installing dotenv with conda in my command prompt, but I had to be explicit about where to get dotenv from. The command that worked was

conda install -c conda-forge python-dotenv

2nd Problem

Once I got dotenv to install I could not set the environment variables from the .env file. The tutorial uses os.path.dirname(__file__) to get the current working directory. It turns out __file__ is always lower case, but my directory has some upper case in it. Consequently the absolute path of the .env file could not be found! I fixed this by using the built-in pathlib module which respects case. Here is some code.

import os
from pathlib import Path
from dotenv import load_dotenv

# Get the base directory
basepath = Path()
basedir = str(basepath.cwd())
# Load the environment variables
envars = basepath.cwd() / '.env'
load_dotenv(envars)
# Read an environment variable.
SECRET_KEY = os.getenv('SECRET_KEY')
Answered By: Mark Kortink

I also have an issue like that. Before that, I run Flask app following the command:

flask run -p 3000

I get advice from here.

python -m flask run [OPTIONS]

Example:

python -m flask run -p 3000

It works for me. Thanks.

Answered By: Rahmat Oktrifianto

I was facing this sort of issue enter image description here

Tried uninstalling python-dotenv and restaslling it multiple times, but nothing helped, finally what helped was very silly,

I deactivated the venv and activated it again.

Now flask run is working fine for me.

FYI :- I am using MacBook Air m1(Apple silicon).

Answered By: Shubham Naik
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.