简体   繁体   中英

Can't access .env file from python

I have django settings.py file and .env in same folder. .env file:

DEBUG=True
SECRET_KEY=123456678910

In settings.py i call theese variables as SECRET_KEY = os.environ.get('SECRET_KEY') . Django says me
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty .
What am I doing wrong?

.env files aren't a feature of Django, Python, or virtualenv. If you want to use them, you'll need to start your app with something that knows about them.

One way of doing this is to install Foreman, create a Procfile, and run it via foreman start ... . If you're planning to deploy to Heroku, use heroku local .

Just try to set this code in your manage.py and wsgi.py files respectively.

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project_name.settings.local")

I have found this solution from Daniel Greenfield's book Two scoops of Django. Hope this will solve your issue.

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM