title: Flask: app.config settings from .env &. flaskenv in mod_wsgi
body: <p>I have spent quite a while trying to figure out how to set .env and .flaskenv configuration values in my flask backend in Google Cloud Platform server. I am using apache2, mod_wsgi, Flask, Python 3.6 and SQLAlchemy. My backend works fine locally on my Mac using pure Flask. </p>
  
  <p>Having python-dotenv installed, running the flask command will set environment variables defined in the files .env and .flaskenv. This, however, does not work with wsgi. The request from apache is redirected to execute my run.wsgi-file. There is no mechanism (that I have knowledge about) to set the environment variables defined in .env and .flaskenv. </p>
  
  <p>The minimun requirement is to pass to the application information if test or development environment should be used. From there I could within <strong>init</strong>.py populate app.config values from an object. However, being somehow able to use config-values from .env and .flaskenv would be far better. I would really appreciate if somebody had any good ideas here - the best practice to set app.config values in wsgi environment.</p>
  
  <p>There are two posts where this same problem has been presented - they really do not have a best practice how to tackle this challenge (and I am sure I am not the only one having a hard time with this):</p>
  
  <p><a href="https://stackoverflow.com/questions/38384454/why-cant-flask-cant-see-my-environment-variables-from-apache-mod-wsgi">Why can't Flask can't see my environment variables from Apache (mod_wsgi)?</a></p>
  
  <p><a href="https://stackoverflow.com/a/9017610">Apache SetEnv not working as expected with mod_wsgi</a></p>
  
  <p>My run.wsgi:</p>
  
  <pre><code>import sys
  sys.path.append("/var/www/contacts-api/venv/lib/python3.6/site-packages")
  sys.path.insert(0,"/var/www/contacts-api/")
  from contacts import create_app
  app = create_app('settings.py')
  app.run()
  </code></pre>
  
  <p>[3]:Allows you to configure an application using pre-set methods.</p>
  
  <pre><code>from flask_appconfig import AppConfig
  def create_app(configfile=None):
  app = Flask('myapp')
  AppConfig(app, configfile)
  return app
  </code></pre>
  
  <p>The application returned by create_app will, in order:</p>
  
  <p>Load default settings from a module called myapp.default_config, if it exists. (method described in <a href="http://flask.pocoo.org/docs/config/#configuring-from-files" rel="nofollow noreferrer">http://flask.pocoo.org/docs/config/#configuring-from-files</a> )
  Load settings from a configuration file whose name is given in the environment variable MYAPP_CONFIG (see link from 1.).
  Load json or string values directly from environment variables that start with a prefix of MYAPP_, i.e. setting MYAPP_SQLALCHEMY_ECHO=true will cause the setting of SQLALCHEMY_ECHO to be True.
  Any of these behaviors can be altered or disabled by passing the appropriate options to the constructor or init_app().</p>
  
  <p>[4]: Using “ENV-only”
  If you only want to use the environment-parsing functions of Flask-AppConfig, the appropriate functions are exposed:</p>
  
  <pre><code>from flask_appconfig.heroku import from_heroku_envvars
  from flask_appconfig.env import from_envvars
  # from environment variables. note that you need to set the prefix, as
  # no auto-detection can be done without an app object
  from_envvars(app.config, prefix=app.name.upper() + '_')
  # also possible: parse heroku configuration values
  # any dict-like object will do as the first parameter
  from_heroku_envvars(app.config)
  </code></pre>