Jonathan Thai
Jonathan Thai

Reputation: 1163

no module named 'dotenv' python 3.8

EDIT: Solved, if anyone comes across this python3.8 -m pip install python-dotenv worked for me.

I've tried reinstalling both dotenv and python-dotenv but I'm still getting the same error. I do have the .env file in the same directory as this script.

#bot.py
import os
import discord

from dotenv import load_dotenv
load_dotenv()


token=os.getenv('DISCORD_TOKEN')

client = discord.Client()


@client.event
async def on_ready():
    print(f'{client.user} has connected to Discord!')


client.run(token)

Upvotes: 106

Views: 323413

Answers (18)

The Flying Maverick
The Flying Maverick

Reputation: 261

This will solve just installing via terminal:

pip3 install python-dotenv for python 3.0 versions or pip install python-dotenv for python different than 3.0

Upvotes: 11

Gonçalo Peres
Gonçalo Peres

Reputation: 13592

Had the same issue on a deployment to my STG environment in Azure App Services

from dotenv import load_dotenv
ModuleNotFoundError: No module named 'dotenv'

And on my requirements.txt I had

python-dotenv>=1.0.0

However, in DEV I was not getting that error.

Accessed the App Service, and noticed that in Settings>Configuration, both environments had different Python versions: in DEV I had Python 3.11, and in STG Python 3.12.

enter image description here

Changing STG to Python 3.11 solved the issue.

Upvotes: 0

BringBackCommodore64
BringBackCommodore64

Reputation: 5470

In my case I was trying to install it as py-dotenv which installs some other package. Which perhaps is the very first version of python-dotenv as it is labeled as 0.1.

Upvotes: 1

Nayem Jaman Tusher
Nayem Jaman Tusher

Reputation: 1078

In your terminal you have to activate your virtual environment and run

pip3 install python-dotenv

for python 3.0 versions . OR,

pip install python-dotenv 

for python bigger than 3.0

Upvotes: 2

Mohammad Azam
Mohammad Azam

Reputation: 43

I was trying all above mentioned options but finally this command worked for me on windows.

py -m pip install python-dotenv

Upvotes: 2

ahmnouira
ahmnouira

Reputation: 3451

Just run

pip install -U python-dotenv

Upvotes: 5

Ekta Sangal
Ekta Sangal

Reputation: 41

For Python 3.8.8 version Anaconda Jupyter

Command to install dotenv library:

pip install python-dotenv

Upvotes: 1

Nour SIDAOUI
Nour SIDAOUI

Reputation: 362

OK so after trying dozens of solutions here's what worked for me on MacOS (python3.10 - dotenv1.0.0) :

pip uninstall python-dotenv

then

python -m pip install python-dotenv

Upvotes: 8

Jakob
Jakob

Reputation: 1216

This is only tangentially related,but maybe it helps some:
python-dotenv is often the first dependency being loaded by your Python application, so if python-dotenv is not found, it might mean that no dependencies are installed at all.

In my case, I had to make sure my Azure Web Application was configured correctly, by setting SCM_DO_BUILD_DURING_DEPLOYMENT and WEBSITES_ENABLE_APP_SERVICE_STORAGE both to true.

Upvotes: 1

Adi Ep
Adi Ep

Reputation: 805

Installing the package globally and not in your virtual environment! pip install python-dotenv

Upvotes: 2

David DiRico
David DiRico

Reputation: 29

I had the same problem, and tried everything here, but once I closed out of my terminal windows and reopened them, the library was finally recognized.

Upvotes: 1

Tien Do
Tien Do

Reputation: 11079

I'm new to Python and just got exact same error. Just changed install command to what I used from a MongoDB tutorial to install PyMongo. It worked like a charm :)

python -m pip install python-dotenv

Upvotes: 131

LFMekz
LFMekz

Reputation: 723

in your installation manager if it's Ubuntu or Debian try: apt install python3-dotenv

you can also try sudo pip3 install dotenv to install via pip.

Whatever you do remember to include explicitly the missing 3 part.

Debian/Ubuntu have separate packages and as of the present time python means python2 and python3 means python3 in their apt repositories. However, when it comes to your locally installed python binary on your system which python binary it defaults to using may vary depending on what /usr/bin/python is symlinked to on your system. Some systems it's symlinked to something like python2.7 and other's it may be something like python3.5. Similar issues exist with locally installed pip. Hence, why using the '3' is important when installing or searching for python packages

Upvotes: 38

Nannigalaxy
Nannigalaxy

Reputation: 627

In my case got this error while running pytest.
Issue got resolved by calling python3 -m pytest <- inside poetry

Upvotes: 1

Adnan Rizwee
Adnan Rizwee

Reputation: 51

I had the same issue (Python 3.8.5, dotenv 0.15.0) and was getting ModuleNotFoundError: No module named 'dotenv' in both the console and JupyterLab. All other packages seemed to install via pip with no problems.

I just ran:

pip3 uninstall python-dotenv

pip3 install -U python-dotenv

Upvotes: 1

mopy
mopy

Reputation: 81

I had the same issue because I was running the installation command locally (in my virtual env).

When running it globally (outside of virtual env) it finally resolved the issue ;)

Upvotes: 4

Yan Bussieres
Yan Bussieres

Reputation: 51

In my case, I was aliasing python to python3, in my zsh console.

Running a .py file with python3 -i filename.py made it work.

Upvotes: 2

mountHouli
mountHouli

Reputation: 131

If you're using poetry (https://python-poetry.org), then...

Be sure you're doing:

$ poetry run SOME_COMMAND
# such as `poetry run pytest`

Instead of just:

$ SOME_COMMAND
# such as `pytest`

My problem (in detail) was...

# Starting with a _contrasting_ example that _unexpectedly worked_..

# A long time ago I did..

$ pip install pytest

# And now, I was doing everything below..

$ poetry add requests

# ..Then I wrote code that has `import requests` in it
# ..Then I wrote some unit test code (to use with pytest) to test the use of `requests`

# ..And NOT knowing I'm supposed to do `poetry run pytest`, I was just doing

$ pytest

# ..And it (oddly) worked, perhaps because maybe `requests` had been installed globally somewhere for me.

# ..But then I did

$ poetry add python-dotenv 

# ..Then I wrote code that had `from dotenv import load_dotenv` in it
# ..Then I wrote some unit test code (to use with pytest) to test the use of `python-dotenv`

# And I got the error I should have gotten..

$ pytest

# ..a bunch of error output, including..
ModuleNotFoundError: No module named 'dotenv'

Thus, the fix was:

# Get rid of the global pytest. Don't want to use that.
# (I think this step is optional, b/c I think `poetry run pytest` below will use the pytest installed via poetry in your virtual env (i.e. I _think_ it will (on it's own) NOT use the globally installed pytest.))
$ pip uninstall pytest

$ poetry add python-dotenv
$ poetry add --dev pytest

$ poetry run pytest

Credit for the fix goes to:

https://github.com/joeyespo/pytest-watch/issues/112

Upvotes: 7

Related Questions