Ang Lee
Ang Lee

Reputation: 11

When deploying Django to Heroku: ModuleNotFoundError: No module named 'dotenv'

When deploying Django to Heroku, I get a ModuleNotFoundError: No module named 'dotenv' error. However, I do have python-dotenv listed in my requirements.txt. So I'm very confused about why Heroku is not happy about dotenv.

Here is my requirements.txt:

cffi==1.14.2
cryptography==3.1
dj-database-url==0.5.0
Django==3.1.1
django-cors-headers==3.5.0
django-heroku==0.3.1
django-rest-knox==4.1.0
django-simple-email-confirmation==0.70
djangorestframework==3.11.1
gunicorn==20.0.4
psycopg2==2.8.6
pycparser==2.20
python-dotenv==0.14.0
pytz==2020.1
six==1.15.0
sqlparse==0.3.1
whitenoise==5.2.0

Upvotes: 0

Views: 2072

Answers (4)

Derek Hill
Derek Hill

Reputation: 6454

The purpose of dotenv is to "read key-value pairs from a .env file and set them as environment variables"

Typically the purpose of having a .env file is to have a convenient way of managing your environment locally.

Typically this is not checked into version control (i.e. it is listed in .gitignore). The benefits are:

  • Your sensitive environment variables can not be seen by others
  • In production you might want to be able to change these variables without re-deploying
  • Different people can run the app in different environments

Therefore when you deploy to Heroku you typically don't include your .env, and so dotenv has nothing to do.

Instead Heroku has its own way of managing environment variables.

This leaves the problem of what to do in your code where trying to call dotenv on Heroku would give errors like you describe.

One option is to only load it if the environment is not production. Something like this:

import os

if not os.environ.get("PRODUCTION"):
    from dotenv import load_dotenv

    load_dotenv()

Note that you need to set the PRODUCTION environment variable on Heroku, or pick a variable that is already there.

Upvotes: 1

Babacar Ly
Babacar Ly

Reputation: 31

Try to uninstall the package and download it again and check that it is in the requirements.txt file.

Upvotes: 0

nightwolfgroup
nightwolfgroup

Reputation: 173

Instead of using python-dotenv try using django-dotenv

Not sure what imports you have in settings.py but try this:

# settings.py
import os
SECRET_KEY = os.getenv('SECRET_KEY')

If you tried to import dotenv at the top of your settings file you may need to remove it.

Also remember to add those environment variables in the 'Config Vars' under the settings of your Heroku app. Those should be there before you push to Heroku.

Upvotes: 1

Jorge López
Jorge López

Reputation: 523

I don´t know if this will help but I had that error when deploying to Heroku, but in my case I was using pipenv, the problem was that the requirements or any import that I did I was installing it in pip install instead of pipenv install, once I did all my installations there it worked, I don´t remember if a did something like pipenv freeze > requirements.txt

Upvotes: 0

Related Questions