Use .env with Flask 1.*


25/03/2019, 23:56
  1. pip install python-dotenv
  2. Create .env inside you app/ folder file with content:
    # FLASK_ENV=production
    FLASK_ENV=development
    
    FLASK_APP=app.py
    SECRET_KEY='your_very_secret_string'
    
  3. Export to your app.py (or whatever you name it)
    # Load .env https://github.com/theskumar/python-dotenv
    from dotenv import load_dotenv, find_dotenv
    load_dotenv(find_dotenv())
    
  4. Now you can get any value from .env like this:
    os.getenv('SECRET_KEY')
    or
    os.getenv('FLASK_ENV')
    ...
    

There are a lots of benefits from using .env inside you project: you ca easily separate environments for development purposes, write conditions depending on which mode you work with, deploy project in to production with easy and comfort... where are really tons of benefits!

For example. You've created .env from this article, now no need manually set environmental variables each time you need run your project, cd to your project and run:

flask run

...and you good to go.

For more detailed info watch following video:



Комментарии