Thanks a lot Graham for your inputs, your suggestion made it work. I was 
able to read the value from "mod_wsg.process_group"

However, I have more environment variables that need to be referred For e.g 
LOGDIR which are like folder paths within the OS, so should I be using 
SetEnv directive in Virtual Hosts Conf files?

Also, whatever SetEnv directive I use in .wsgi file, how do I extract the 
same in my Python code

On Friday, 15 November 2019 12:09:33 UTC+5:30, Vaibhav Bajpai wrote:

> 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
>
> *bbmetrics.wsgi*
>
> #!/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 
> <http://www.google.com/url?q=http%3A%2F%2F172.27.138.173%3A56868&sa=D&sntz=1&usg=AFQjCNGv87sckIHsubHFaPLf2tvjzacO6A>]
>  Traceback (most recent call last):
> [Thu Nov 14 23:59:09.601199 2019] [wsgi:error] [pid 73322] [client 
> xx.xx.xx.xx:xxxx 
> <http://www.google.com/url?q=http%3A%2F%2F172.27.138.173%3A56868&sa=D&sntz=1&usg=AFQjCNGv87sckIHsubHFaPLf2tvjzacO6A>]
>    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 
> <http://www.google.com/url?q=http%3A%2F%2F172.27.138.173%3A56868&sa=D&sntz=1&usg=AFQjCNGv87sckIHsubHFaPLf2tvjzacO6A>]
>      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 
> <http://www.google.com/url?q=http%3A%2F%2F172.27.138.173%3A56868&sa=D&sntz=1&usg=AFQjCNGv87sckIHsubHFaPLf2tvjzacO6A>]
>  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 [email protected]
>
>                 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()
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"modwsgi" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/modwsgi/778d61d1-bc3a-42fa-9df0-1bdb7db2de9a%40googlegroups.com.

Reply via email to