Justin Henson
Justin Henson

Reputation: 53

ModuleNotFoundError: No module named 'dotenv' error in Jupyter Notebook

After running this cell in my Jupyter Notebook:

# get api key from your .env file
import os
from dotenv import load_dotenv

load_dotenv()
API_KEY = os.getenv('NASDAQ_API_KEY')

print(API_KEY)

I get this error message when I run the cell:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
/var/folders/px/ppxppr6d63n4bxqc03x741th0000gn/T/ipykernel_6117/3153037547.py in <module>
      1 # get api key from your .env file
      2 import os
----> 3 from dotenv import load_dotenv
      4 
      5 load_dotenv()

ModuleNotFoundError: No module named 'dotenv'

I have installed, uninstalled, and reinstalled the 'python-dotenv' module, but I still have the same error message:

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

My environment is setup as follows:

$ dotenv --version                 
dotenv, version 0.21.1
$ pip --version
pip 20.3.4 from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip (python 2.7)
$ pip3 --version
pip 23.0 from /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pip (python 3.10)
$ python3 --version
Python 3.10.5
$ pipenv --version
pipenv, version 2022.7.4

Also pip freeze output shows python-dotenv.

pip freeze
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
certifi==2021.10.8
chardet==4.0.0
click==7.1.2
dotenv==0.0.5
idna==2.10
python-dotenv==0.18.0
requests==2.27.1
typing==3.10.0.0
urllib3==1.26.14

My .env file is located in the root directory of my project and my Jupyter Notebook:

$ ls -al
total 40
drwxr-xr-x   9 user  staff   288 Feb 11 13:27 .
drwxr-xr-x   5 user  staff   160 Jan  9 09:03 ..
-rw-r--r--   1 user  staff    38 Feb 11 12:08 .env
drwxr-xr-x  13 user  staff   416 Jan 29 14:52 .git
-rw-r--r--   1 user  staff  3088 Feb 11 12:15 .gitignore
drwxr-xr-x  14 user  staff   448 Feb  3 14:26 DataScienceGuidedCapstone
-rw-r--r--   1 user  staff   117 Jan  6 14:08 README.md
drwxr-xr-x   6 user  staff   192 Feb 11 13:23 Unit-7-API-Mini-Project
$ ls -al
total 48
drwxr-xr-x  7 user  staff   224 Feb 11 13:28 .
drwxr-xr-x  9 user  staff   288 Feb 11 13:27 ..
-rw-r--r--  1 user  staff    38 Feb 11 13:28 .env
drwxr-xr-x  3 user  staff    96 Feb 11 11:39 .ipynb_checkpoints
-rw-r--r--@ 1 user  staff  9237 Feb 11 13:23 api_data_wrangling_mini_project.ipynb
-rw-r--r--@ 1 user  staff    35 Feb 10 16:44 env_example.txt
-rw-r--r--  1 user  staff    36 Feb 11 12:09 python-dotenv

Screen shot of error in Jupyter Notebook: jupyter notebook error

I would sincerely appreciate any suggestions. Thanks.

Upvotes: 0

Views: 2747

Answers (2)

Justin Henson
Justin Henson

Reputation: 53

So I updated conda and installed the python-dotenv package through conda and it worked:

$ conda update -n base -c defaults conda

$ conda install python-dotenv                                   
Collecting package metadata (current_repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: /usr/local/anaconda3

  added / updated specs:
    - python-dotenv


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    python-dotenv-0.21.1       |     pyhd8ed1ab_0          22 KB  conda-forge
    ------------------------------------------------------------
                                           Total:          22 KB

The following NEW packages will be INSTALLED:

  python-dotenv      conda-forge/noarch::python-dotenv-0.21.1-pyhd8ed1ab_0 


Proceed ([y]/n)? y


Downloading and Extracting Packages
                                                                                
Preparing transaction: done
Verifying transaction: done
Executing transaction: done

Upvotes: 0

avnogy
avnogy

Reputation: 31

It looks like you have installed the dotenv package for python 2.7 (from the pip freeze) but you are using python 3 to run the code.

Upvotes: 2

Related Questions