Oleg Somov
Oleg Somov

Reputation: 905

Flask doesn't see environment variables

I connected apache with my Flask App with WSGI with virtualenv.I have a couple configuration variables(secret key and database URI), so I'm using

app = Flask(__name__)
app.config.from_envvar('CONFIG_FILE_FLASK')

The error in apache errorlog is:

RuntimeError: The environment variable 'CONFIG_FILE_FLASK' is not set and as such configuration could not be loaded. Set this variable and make it point to a configuration file

This variable exists (using command printenv) :

......
UPSTART_INSTANCE=
COLORTERM=truecolor
CONFIG_FILE_FLASK=/var/flaskconf/config.cfg

SSH_AUTH_SOCK=/run/user/1000/keyring/ssh
XDG_GREETER_DATA_DIR=/var/lib/lightdm-data/oleg
USER=oleg
......

/var/flaskconf/config.cfg itself is :

SECRET_KEY = 'thisissupposedtobesecret'
SQLALCHEMY_DATABASE_URI = 'mysql://user:password@localhost/Table'

It also doesn't work if I do this:

sudo python __init__.py 

BUT, if without sudo it works!(maybe it'll be a clue)

Thank you so much in advance. Ready to provide any file immediately.

UPDATE 1 I also noticed that variable FLASK_APP also "invisible"

oleg@test:/var/www/app/app$ export FLASK_APP=app.py
oleg@test:/var/www/app/app$ flask run
Usage: flask run [OPTIONS]

Error: The file/path provided (app) does not appear to exist.  Please verify the path is correct.  If app is not on PYTHONPATH, ensure the extension is .py
oleg@test:/var/www/app/app$ 

Any ideas about that?

EDIT: File structure:

Upvotes: 2

Views: 4288

Answers (1)

Oleg Somov
Oleg Somov

Reputation: 905

The problem solved by making my Flask application a python module.

First of all the structure of application now is next (correct structure):

And the __init__.py includes following:

from .iotdev import app

Also to work with the virtual environment, site configuration file was edited by adding WSGIDaemonProcess AND WSGIProcessGroup statement, so the iotdev.conf(the full path to file is /etc/apache2/sites-available/iotdev.conf):

<VirtualHost *:80>
    ServerName 127.0.0.1
    ServerAdmin [email protected]  
    WSGIDaemonProcess iotdev python-path=/var/www/iotdev:/var/www/iotdev/iotdev/env/lib/python2.7/site-packages
    WSGIProcessGroup iotdev
    WSGIScriptAlias / /var/www/iotdev/iotdev.wsgi
    <Directory /var/www/iotdev/iotdev/>
        Order allow,deny
        Allow from all
    </Directory>
    Alias /static /var/www/iotdev/iotdev/static
    <Directory /var/www/iotdev/iotdev/static/>
        Order allow,deny
        Allow from all
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Thanks everybody who was helping me with this issue, you're pushed me to some thoughts and ideas that make this working.

Upvotes: 2

Related Questions