Why can I not import load_dotenv?
Asked Answered
G

12

24

I'm trying to code a simple discord bot in python, but it says i cant import load_dotenv, instead giving me this error:

Traceback (most recent call last):
  File "/home/pi/Rotomi/Rotomi.py", line 5, in <module>
    from dotenv import load_dotenv
ImportError: cannot import name 'load_dotenv' from 'dotenv' (/home/pi/.local/lib/python3.7/site-packages/dotenv/__init__.py)

For reference, here is my current code: https://pastebin.com/75qru00R

Giorgia answered 7/1, 2020 at 5:50 Comment(4)
there are two modules pip install dotenv and pip install python-dotenv (which have load_dotenv). Maybe you installed wrong moduleWire
Ahh, that’s probably it. Thank you!Giorgia
correction there is dotenv, dotenv-python and python-dotenv (and few others). python-dotenv has load_dotenvWire
@Wire yeesh... that collection sounds ripe for a typosquatting attack.Deuteronomy
C
20

Just ran into the same issue and luckily we got it solved.

The problem is the package itself. So instead of install package dotenv, what you should install is python-dotenv.

pip install python-dotenv

After that, you code shall work

Crackdown answered 16/12, 2021 at 18:49 Comment(1)
just to add to the confusion... it's python-dotenv NOT dotenv-pythonDepurative
P
6

Since load_dotenv is declared in main.py of the package, I simply imported it and used the function like that and it worked.

from dotenv import main
import os

main.load_dotenv()

print(os.getenv('access_token'))
Paulson answered 3/12, 2021 at 11:6 Comment(0)
S
6

In my case : I uninstalled dotenv-python and dotenv first by following commend

pip uninstall dotenv-python
pip uninstall dotenv

and after install python-dotenv module by

pip install python-dotenv
Smothers answered 7/7, 2022 at 7:47 Comment(1)
Worked for me with pipenv. There were some problems with a locally and globally installed version as well but after uninstalling and reinstalling everything it worked.Essene
F
4

I don't know if this still needs to be solved, but I found out that this worked for me:

from dotenv import dotenv_values
temp = dotenv_values(".env")
TOKEN = temp["DISCORD_TOKEN"] 
Folio answered 6/1, 2021 at 3:18 Comment(0)
F
2

I came here as I faced the same issue. I did my code according to this tutorial but

from dotenv import load_dotenv

caused this error:

*** ImportError: cannot import name 'load_dotenv' from 'dotenv' (/usr/local/lib/python3.7/site-packages/dotenv.py)

The import itself worked:

(Pdb) import dotenv

But the suggestion didn't:

(Pdb) from dotenv import Dotenv
*** ImportError: cannot import name 'Dotenv' from 'dotenv' (/usr/local/lib/python3.7/site-packages/dotenv.py)

Apparently also in my case I had to complete another install: pip3.7 install python-dotenv --user. Yet, from dotenv import Dotenv would not run. The tutorial is correct in stating:

from dotenv import load_dotenv

I assume this could be a Python 2/3 transition error.

Frederiksen answered 20/4, 2020 at 12:38 Comment(0)
M
1

I'm almost embarrassed to write this but I had forgotten to close then re-open VSC after running the correct command:

pip install python-dotenv

then

from dotenv import load_env
Morbidity answered 24/1, 2022 at 12:35 Comment(0)
O
1

For me, uninstalling and reinstalling python-dotenv fixed the issue.

Oat answered 27/12, 2023 at 23:46 Comment(0)
C
0

According to there official docs from https://github.com/pedroburon/dotenv , You import Dotenv not load_dotenv.

>>> from dotenv import Dotenv
>>> dotenv = Dotenv('/path/to/.env')
>>> print dotenv
{"FOO": "bar", "Bar": "baz"}

Coadjutor answered 7/1, 2020 at 6:13 Comment(0)
S
0

Python 3.6.9

With api-token = '123456' in the .env file in the root of your project.

import dotenv
dotenv.load_dotenv()
import os
token = os.environ.get("api-token")

print(token)
Stability answered 17/1, 2022 at 20:5 Comment(0)
M
0

My propblem was that my IDE was using the wrong python version .

Fixed here:

https://bobbyhadz.com/blog/python-no-module-named-dotenv

Milliemillieme answered 22/5, 2023 at 17:22 Comment(0)
S
0

Simply import from from main class/sub-package like this:

from dotenv.main import load_dotenv

Configuration file for the application:

dotenv_path = Path(".local.env")
load_dotenv(dotenv_path=dotenv_path)

Maybe in your config.py

Selffulfillment answered 28/6, 2023 at 20:33 Comment(0)
S
0

I had the same symptom. My problem was a file tokenize.py, which overrode Python's tokenize module. Renaming my file fixed this problem.

Sourpuss answered 17/7, 2023 at 23:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.