[Tutor] Environment variables and Flask

Alan Gauld alan.gauld at yahoo.co.uk
Fri Jun 28 04:34:40 EDT 2019


On 28/06/2019 06:24, Mayo Adams wrote:

> What are these environment variables, exactly, and why is it necessary to
> set them? 

When you run a program under an operating system the OS sets up an
"environment" (or context) for the program to run in. (This
includes the OS shell that the user interacts with - each user
gets their own environment.)

Environment variables are variables that are accessible within
that environment but not from other environments. Thus two users
may have different values for the same variable, such as the HOME
variable which dictates the users home directory. Or PATH which
tells the OS where the users programs can be found.

When you start a new program the OS creates a copy of the current
environment(your user environment) and runs the program within
that copy. Thus if the program modifies any of the environment
variables it does not affect the parent process environment
since it is modifying its own copy. (So if it changes HOME to
give itself its own default directory that doesn't change the
user's HOME - or any other program environment for that matter)

Some applications define their own environment variables as a way
of setting user specific values. So a web server might define
the root directory for the server as an environment variable
then each user can set a different root without having to pass
that in each time they run the program.

Environment variable have fallen out of favour for user settings
and config files are now preferred. But some things are a bit
easier via en environment variable - especially where you spawn
new sub-processes and don't want the sub-process to have
to re-read the config file each time.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list