6

I tried to set up sensitive information as environment variables in CentOS, and pass them to Flask config file used in main file, i.e. init.py . But it did not work. The Flask application is running under Apache.

I first edit /etc/environment as root user

MAIL_USERNAME="abcde@abc.com"

then logout, login again Then verify MAIL_USERNAME is set by running

echo $MAIL_USERNAME

This works fine

And in configuration.py, this is how I set MAIL_USERNAME.

MAIL_USERNAME = os.environ.get('MAIL_USERNAME')

for testing purpose, I print out MAIL_USERNAME

in __init__.py
print(MAIL_USERNAME)

Then from terminal, if I run

python3.4 __init__.py

it print out correct values of MAIL_USERNAME

However, if I tested on web browser, MAIL_USERNAME is just not set. it shows NONE. I verify this by looking Apache log.

Any idea of how this works would be really appreciated.

Thanks

DBS
  • 103
  • 1
  • 1
  • 7
  • Is it possible Apache was started before the environment variables were set, and so all the python workers that are being spawned from Apache are only getting the environmental variables that were defined at the time of launching Apache? – Christopher Shroba Nov 17 '16 at 05:52
  • Came across this question as I had a similar issue. For me, the solution was to restart my terminal so that the new variables got loaded. – 321k Nov 06 '19 at 11:53

3 Answers3

3

With your CLI, set the environment variable as you want. On Linux and macOS, this is done with export KEY=value.

After that, the environment variable KEY will be available for your Python script or Flask app via os.environ.get('KEY'), like this:

import os
print os.environ.get('key')

>>> value
Jochem Schulenklopper
  • 6,452
  • 4
  • 44
  • 62
Saif ali Karedia
  • 860
  • 1
  • 8
  • 15
0

I had a very similar problem because I used PyCharm terminal to run flask. A similar issue was described and solved here. My solution was switching to regular cmd (I worked on Windows 10) and just running everything there:

>> set MAIL_USERNAME='bla@example.com'
... (other env variables sets)
>> py manage.py runserver (I run my flask app through a manage script)

I could successfully send an email using my flask app - all the environment variables used in the app were read correctly.

On Linux you can just use export instead of set. I hope it helps.

Leo Skhrnkv
  • 1,513
  • 16
  • 27
0

Maybe you can use Apache directive PassEnv as mentioned here on Apache's official web documenting how to use environment variables.

There are two kinds of environment variables that affect the Apache HTTP Server.

First, there are the environment variables controlled by the underlying operating system. These are set before the server starts. They can be used in expansions in configuration files, and can optionally be passed to CGI scripts and SSI using the PassEnv directive.

Second, the Apache HTTP Server provides a mechanism for storing information in named variables that are also called environment variables. This information can be used to control various operations such as logging or access control. The variables are also used as a mechanism to communicate with external programs such as CGI scripts. This document discusses different ways to manipulate and use these variables.

Although these variables are referred to as environment variables, they are not the same as the environment variables controlled by the underlying operating system. Instead, these variables are stored and manipulated in an internal Apache structure. They only become actual operating system environment variables when they are provided to CGI scripts and Server Side Include scripts. If you wish to manipulate the operating system environment under which the server itself runs, you must use the standard environment manipulation mechanisms provided by your operating system shell.

I make some of the text cited above bold to make things clearer and maybe easier to explain.

Hope this helps.

Community
  • 1
  • 1
kxu
  • 73
  • 4