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 string.
I also want to know how to read the environment variables in python (in the later part of the script) once I set it.
>>>import os
>>>'HOME'in os.environ # Check an existing env. variableTrue...
Python 2
>>>import os
>>> os.environ.has_key('HOME')# Check an existing env. variableTrue>>> os.environ.has_key('FOO')# Check for a non existing variableFalse>>> os.environ['FOO']='1'# Set a new env. variable (String value)>>> os.environ.has_key('FOO')True>>> os.environ.get('FOO')# Retrieve the value'1'
os.environ behaves like a python dictionary, so all the common dictionary operations can be performed. In addition to the get and set operations mentioned in the other answers, we can also simply check if a key exists. The keys and values should be stored as strings.
Python 3
For python 3, dictionaries use the in keyword instead of has_key
>>> import os
>>> 'HOME' in os.environ # Check an existing env. variable
True
...
Python 2
>>> import os
>>> os.environ.has_key('HOME') # Check an existing env. variable
True
>>> os.environ.has_key('FOO') # Check for a non existing variable
False
>>> os.environ['FOO'] = '1' # Set a new env. variable (String value)
>>> os.environ.has_key('FOO')
True
>>> os.environ.get('FOO') # Retrieve the value
'1'
There is one important thing to note about using os.environ:
Although child processes inherit the environment from the parent process, I had run into an issue recently and figured out, if you have other scripts updating the environment while your python script is running, calling os.environ again will not reflect the latest values.
This mapping is captured the first time the os module is imported,
typically during Python startup as part of processing site.py. Changes
to the environment made after this time are not reflected in
os.environ, except for changes made by modifying os.environ directly.
os.environ.data which stores all the environment variables, is a dict object, which contains all the environment values:
>>> type(os.environ.data) # changed to _data since v3.2 (refer comment below)
<type 'dict'>
You should assign string value to environment variable.
os.environ["DEBUSSY"] = "1"
If you want to read or print the environment variable just use
print os.environ["DEBUSSY"]
This changes will be effective only for the current process where it was assigned, it will no change the value permanently. The child processes will automatically inherit the environment of the parent process.
import os
os.environ["variable_1"]="value_1"
os.environ["variable_2"]="value_2"# To Verify above code
os.environ.get("variable_1")
os.environ.get("variable_2")
I have been trying to add environment variables. My goal was to store some user information to system variables such that I can use those variables for future solutions, as an alternative to config files.
However, the method described in the code below did not help me at all.
import os
os.environ["variable_1"] = "value_1"
os.environ["variable_2"] = "value_2"
# To Verify above code
os.environ.get("variable_1")
os.environ.get("variable_2")
This simple code block works well, however, these variables exist inside the respective processes such that you will not find them in the environment variables tab of windows system settings. Pretty much above code did not serve my purpose. This problem is discussed here: variable save problem
os.environ.putenv(key, value)
Another unsuccessful attempt. So, finally, I managed to save variable successfully inside the window environment register by mimicking the windows shell commands wrapped inside the system class of os package. The following code describes this successful attempt.
os.system("SETX {0} {1} /M".format(key, value))
I hope this will be helpful for some of you.
回答 8
应该注意的是,如果您尝试将环境变量设置为bash评估,它将不会存储您期望的结果。例:
from os import environ
environ["JAVA_HOME"]="$(/usr/libexec/java_home)"
It should be noted that if you try to set the environment variable to a bash evaluation it won’t store what you expect. Example:
from os import environ
environ["JAVA_HOME"] = "$(/usr/libexec/java_home)"
This won’t evaluate it like it does in a shell, so instead of getting /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home as a path you will get the literal expression $(/usr/libexec/java_home).
Make sure to evaluate it before setting the environment variable, like so:
from os import environ
from subprocess import Popen, PIPE
bash_variable = "$(/usr/libexec/java_home)"
capture = Popen(f"echo {bash_variable}", stdout=PIPE, shell=True)
std_out, std_err = capture.communicate()
return_code = capture.returncode
if return_code == 0:
evaluated_env = std_out.decode().strip()
environ["JAVA_HOME"] = evaluated_env
else:
print(f"Error: Unable to find environment variable {bash_variable}")
You can use the os.environ dictionary to access your environment variables.
Now, a problem I had is that if I tried to use os.system to run a batch file that sets your environment variables (using the SET command in a **.bat* file) it would not really set them for your python environment (but for the child process that is created with the os.system function). To actually get the variables set in the python environment, I use this script:
import re
import system
import os
def setEnvBat(batFilePath, verbose = False):
SetEnvPattern = re.compile("set (\w+)(?:=)(.*)$", re.MULTILINE)
SetEnvFile = open(batFilePath, "r")
SetEnvText = SetEnvFile.read()
SetEnvMatchList = re.findall(SetEnvPattern, SetEnvText)
for SetEnvMatch in SetEnvMatchList:
VarName=SetEnvMatch[0]
VarValue=SetEnvMatch[1]
if verbose:
print "%s=%s"%(VarName,VarValue)
os.environ[VarName]=VarValue