Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

does not load env variables from the .env file #76

Closed
musicformellons opened this issue Jan 30, 2016 · 7 comments
Closed

does not load env variables from the .env file #76

musicformellons opened this issue Jan 30, 2016 · 7 comments

Comments

@musicformellons
Copy link

I have an .env file (in same folder as manage.py) with:
DJANGO_DEBUG=False
DATABASE_URL=postgres://user:pw@127.0.0.1:5432/dbname
POSTGRES_PASSWORD=pw
POSTGRES_USER=user
DJANGO_ADMIN_URL=r'^admin/'
DJANGO_SETTINGS_MODULE=config.settings.production
DJANGO_SECRET_KEY=7878078078078907890789087etc
DJANGO_ALLOWED_HOSTS=email.com
DJANGO_SERVER_EMAIL=info@email.com
DJANGO_SECURE_SSL_REDIRECT=False

But when I do:
python manage.py migrate --settings config.settings.production

I get:
django.core.exceptions.ImproperlyConfigured: Set the DJANGO_SECRET_KEY environment variable

Also env does not show any of the variables. The variables should have been loaded from my .env file right? So what's wrong?

BTW: I think the docs could be clearer on where to put the .env file and that is is loaded and when, I suppose. I looked elsewhere to find that that was indeed to be expected.

@musicformellons
Copy link
Author

Solved it. The .env needs to be in the same folder as the settings files, not the manage.py file. Confusing as in the the Django Cookiecutter the env.example file is in the manage.py folder...

@tamj0rd2
Copy link

tamj0rd2 commented Oct 20, 2016

@musicformellons Would you mind sharing what your settings.py looks like? I'm having this issue and can't work it out
Edit: Literally just figured it out. I was missing environ.Env.read_env()

@metakermit
Copy link

I was having some problems with this too, but after specifying in settings.py the absolute path to where I keep my .env file (next to manage.py), running all the manage.py commands works.

base = environ.Path(__file__) - 2 # two folders back (/a/b/ - 2 = /)
environ.Env.read_env(env_file=base('.env')) # reading .env file

@alexraju91
Copy link

Modified this bit more so that if ENV_PATH is present, environment variables will be loaded from this file.

env_file = '.env'
if not env.str('ENV_PATH', '.env') == '.env':
    env_file = env.str('ENV_PATH', '.env') + env_file

env.read_env(env_file=base(env_file))

@variable
Copy link
Contributor

variable commented Nov 15, 2019

I have a PR to add ability to point to a env file via environment variable.

#242

@efojs
Copy link

efojs commented May 10, 2021

Guys 🤦‍♂️
Spaces around = (equal sign) in .env file caused the same problem and nothing helped:
SECRET_KEY = <secret_key>

Removing spaces solved the problem:
SECRET_KEY=<secret_key>

@shaheem-pp
Copy link

Solved it. The .env needs to be in the same folder as the settings files, not the manage.py file. Confusing as in the the Django Cookiecutter the env.example file is in the manage.py folder...

But I still have the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants