'How do I pass env variables to uwsgi?

I have a WSGI handler configured under Apache and I'm defining some environmental variables in the Apache virtual host configuration.

SetEnv APP_CONFIG "/var/lib/myapp/app.config"
SetEnv LOG_CONFIG "/var/lib/myapp/app.logging.yml"

To test the handler in development without having to install and configure Apache I'm using uWSGI with the --http option.

uwsgi --http :9090 --ini uwsgi.ini --wsgi-file wsgi.py

wsgi.py

def application(environ, start_response):
    config_file_path = environ['APP_CONFIG']
    start_response('200 OK', [('Content-Type','text/html')])
    return ["Hello World"]

Using the uWSGI http server how can I pass these variables to my application as part of the environ argument?

I have tried setting environmental variables in the uwsgi.ini file:

[uwsgi]
env = APP_CONFIG="/var/lib/myapp/app.config"

but I get:

File "wsgi.py", line 5, in application
config_file_path = environ['APP_CONFIG']
KeyError: 'APP_CONFIG'


Solution 1:[1]

[uwsgi]
env             = RAY_REDIS_PASS=ray_pass
env             = RAY_REDIS_PORT=6380
strict          = true
chdir           = /Users/judas/projects/myapp
master-fifo     = /tmp/myapp_fifo0
master-fifo     = /tmp/myapp_fifo1
module          = myapp.wsgi:application
master          = true
vacuum          = true
need-app        = true
processes       = 4
die-on-term     = true
procname-prefix = myapp
harakiri        = 30
socket          = /tmp/myapp_uwsgi.sock
lazy-apps       = true
logger          = file:logfile=/tmp/apps.log,maxsize=2000000000
import          = postfork

Solution 2:[2]

As pointed in https://stackoverflow.com/a/18490958/3383797 do not use whitespaces:

[uwsgi]
env=MY_VAR_NAME=foobar

Solution 3:[3]

I think you just need to specify you .ini file

uwsgi --ini uwsgi.ini --http = :9090 --wsgi-file wsgi.py

Solution 4:[4]

I discovered how to do this using addvar in the wsgi.ini:

[uwsgi]
route-run = addvar:APP_CONFIG="/var/lib/myapp/app.config"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Saigesp
Solution 3 Rafal Janik
Solution 4 Phill