Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set environment variables in Python?

I need to set some environment variables in the Python script and I want all the other scripts that are called from Python to see the environment variables' set.

If I do,

os.environ["DEBUSSY"] = 1 

it complains saying that 1 has to be a string.

I also want to know how to read the environment variables in Python (in the latter part of the script) once I set it.

like image 609
prahallada Avatar asked May 11 '11 22:05

prahallada


People also ask

What is the use of ENVIRON variable in Python?

When the os module is loaded by Python interpreter, the environ value is set. Any further changes in the environment variables through external programs will not get reflected in the already running Python program. The os.environ variable is a dictionary-like object. If we print it, all the environment variables name and values will get printed.

How to get the Dictionary of all environment variables in Python?

You can think of environment variables as a dictionary, where the key is the environment variable name and the value is the environment variable value. We can use Python os module “environ” property to get the dictionary of all the environment variables. When the os module is loaded by Python interpreter, the environ value is set.

How do I add environment variables in Python decouple?

First install Python Decouple into your local Python environment. Once installed, create a .env file in the root of your project which you can then open up to add your environment variables. ‌You can then add your environment variables like this: Then save (WriteOut) the file and exit nano.

What are environment variables in Linux?

Environment variables is the set of key-value pairs for the current user environment. They are generally set by the operating system and the current user-specific configurations. For example, in a Unix environment, the environment variables are set using the user profile i.e..bash_profile,.bashrc, or.profile files.


2 Answers

Environment variables must be strings, so use

os.environ["DEBUSSY"] = "1" 

to set the variable DEBUSSY to the string 1.

To access this variable later, simply use:

print(os.environ["DEBUSSY"]) 

Child processes automatically inherit the environment variables of the parent process -- no special action on your part is required.

like image 190
Sven Marnach Avatar answered Sep 24 '22 06:09

Sven Marnach


You may need to consider some further aspects for code robustness;

when you're storing an integer-valued variable as an environment variable, try

os.environ['DEBUSSY'] = str(myintvariable) 

then for retrieval, consider that to avoid errors, you should try

os.environ.get('DEBUSSY', 'Not Set') 

possibly substitute '-1' for 'Not Set'

so, to put that all together

myintvariable = 1 os.environ['DEBUSSY'] = str(myintvariable) strauss = int(os.environ.get('STRAUSS', '-1')) # NB KeyError <=> strauss = os.environ['STRAUSS'] debussy = int(os.environ.get('DEBUSSY', '-1'))  print "%s %u, %s %u" % ('Strauss', strauss, 'Debussy', debussy) 
like image 28
Mark Avatar answered Sep 24 '22 06:09

Mark