How to set an integer as a environment variable?
batch-file, python, windows
Solution
Environment variables can only contain string values. You must do something like this:
os.environ['BUILD_VER'] = str(row)
However, you have to understand that any environment variable you set will only take effect for the Python process and any process that it spawns after setting it.
You can not use Python to set environment variable from a shell script, for instance, because the shell spawns Python and Python cannot set environment variables for the shell that spawned it.
If you are trying to set a Windows environment variable to a value that you are generating in Python, you should have a Python script that prints the value and have your batch file execute the script. Let's say your script `getver.py` looks like this:
row = 399
print row
Then your batch file would have something like this in it:
FOR /F %v IN ('getver.py') DO SET BUILD_VER=%v
Problem
I am trying to set an environment variable as below ,however row has an integer variable and running into following error,how to fix this?or is there a better way to set the environment variable using python ``` row = 399 os.environ['BUILD_VER'] = row ``` ERROR:- ``` os.environ['BUILD_VER'] = row File "C:\Python27\lib\os.py", line 420, in __setitem__ putenv(key, item) TypeError: must be string, not int ```