python-dotenv: ImportError: cannot import name load_dotenv

I’ve been getting this error on a python 2.7 installation:

ImportError: cannot import name load_dotenv

any word on what gives?

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 9
  • Comments: 34 (6 by maintainers)

Most upvoted comments

@cnicodeme the correct install command is:

pip install python-dotenv

You can simply fix this by

pip uninstall dotenv
pip install python-dotenv

Let me know if that helps!

Cheers!

Ok for future peoples that get here, the load_dotenv has been removed.

Do this instead :

import os
from dotenv import Dotenv
dotenv = Dotenv(os.path.join(os.path.dirname(__file__), ".env")) # Of course, replace by your correct path
os.environ.update(dotenv)

After uninstall dotenv, for my case it still didn’t work. However, it works after I reinstall python-dotenv.

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

I also got the error ImportError: cannot import name load_dotenv when using Python 3.6 after installing https://github.com/theskumar/python-dotenv version 0.6.4 to with pip install python_dotenv.

I gave up and tried https://github.com/mattseymour/python-env version 1.0.0 instead, which I installed with pip install python-env. I used it in my file as follows, and it worked successfully without any issues:

import os
import dotenv

# output of the following command should be: `['__author__', '__builtins__', '__cached__', ...`
print("dotenv should be shown here: ", dir(dotenv))

APP_ROOT = os.path.join(os.path.dirname(__file__), '')
dotenv_path = os.path.join(APP_ROOT, '.env')
dotenv.load(dotenv_path)
fb_page_token = os.getenv('FACEBOOK_PAGE_TOKEN')

@cnicodeme that’s kind of strange as Dotenv was never part of this library. Are you sure, the library that you are using is python-dotenv and not something else.

It would really helpful if you can post the output of your pip freeze command and/or

import dotenv
print dir(dotenv)

Thanks,

@sirvon well make sure you have activated your virtualenv if you are using this inside it.

sudo apt install python3-dotenv worked for me in a Hyper-V virtual machine with Ubuntu 18.04

Uninstall dotenv and python-dotenv sudo pip uninstall dotenv && sudo pip uninstall python-dotenv then install the python-dotenv sudo pip install python-dotenv

This really works

@shaunakmukherjee try uninstalling and installing pip install python-dotenv.

Best option would be to create and try inside a virtualenv.