I have installed python-dotenv but python cannot find it

Mark Kortink picture Mark Kortink · Nov 19, 2019 · Viewed 12.1k times · Source

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.

Answer

Greg Cowell picture Greg Cowell · Nov 20, 2019

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.