Iam working on running a Python Flask app with Apache HTTP Server (v2.4) where Iam attempting to setup an environment variable to identify deploy environment type (Dev, Prod) and unable to extract the environment variable via "os.environ" in python code
After following few posts, i made the following changes I made in key files of my code. Please help me with a solution. Any suggestions and workarounds would be appreciated
#!/usr/bin/python3
import os
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0, "/var/www/bbmetrics")
def application(environ, start_response):
os.environ['CE360AF_ENV'] = environ['CE360AF_ENV']
from bbmetrics import app as _application
application.secret_key = 'bbmetrics_secret'
return _application(environ, start_response)
I get the error as below when running the app Request URLs but the Python interpreter confirms that variable is correctly set
mod_wsgi (pid=73322): Exception occurred processing WSGI script '/var/www/bbmetrics/bbmetrics.wsgi'.
[Thu Nov 14 23:59:09.601165 2019] [wsgi:error] [pid 73322] [client xx.xx.xx.xx:xxxx] Traceback (most recent call last):
[Thu Nov 14 23:59:09.601199 2019] [wsgi:error] [pid 73322] [client xx.xx.xx.xx:xxxx] File "/var/www/bbmetrics/bbmetrics.wsgi", line 10, in application
[Thu Nov 14 23:59:09.601204 2019] [wsgi:error] [pid 73322] [client xx.xx.xx.xx:xxxx] os.environ['CE360AF_ENV'] = environ['CE360AF_ENV']
[Thu Nov 14 23:59:09.601223 2019] [wsgi:error] [pid 73322] [client xx.xx.xx.xx:xxxx] KeyError: 'CE360AF_ENV'
[dc-user@xxxxxx bbmetrics]$ python3
Python 3.7.4 (default, Oct 25 2019, 03:53:56)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36.0.1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> print(os.environ['CE360AF_ENV'])
dev
>>>
__init__.py
#!/usr/bin/python3
from flask import Flask
app = Flask(__name__)
from bbmetrics.bbmetrics import *
Virtual Host Configiuration File
<VirtualHost *:80>
ServerName x.x.x.x
ServerAlias xxxxx.com
ServerAdmin a...@pqr.com
WSGIScriptAlias / /var/www/bbmetrics/bbmetrics.wsgi
<Directory /var/www/bbmetrics/bbmetrics/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/bbmetrics/bbmetrics/static
<Directory /var/www/bbmetrics/bbmetrics/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog /var/log/httpd/error.log
LogLevel warn
CustomLog /var/log/httpd/access.log combined
</VirtualHost>
Calling Python code for extracting variable. Is this the right way to extract environment variable for WSGI based Python Flask App after WSGI file is correctly written
#!/usr/bin/python3
import os
import requests
import datetime
import configparser
from flask import abort
httpErrorCode = {
500: "server Error",
404: "Resource Not Found",
401: "Not Authorized"
}
deployEnv = os.environ['CE360AF_ENV']
app.run() Python File
#!/usr/bin/python3
from flask import request
from flask_restful import Resource, Api, reqparse
from flask_cors import CORS
from bbmetrics import app
from bbmetrics.main import GetProjectRepos,GetAllPullRequestMetricsFromRepo,GetAllPullRequestMetricsFromRepoForDateRange
api = Api(app)
CORS(app)
@app.route("/")
def welcome():
return "Welcome to CE360 Assessment Framework!!!"
if __name__ == '__main__':
app.run()