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
It appears an Orb is being used in this case, if the Orb defines the variable type as integer , it won't be able to accept an environment variable. The parameter type for a parameter you'd like to assign as an environment variable is env_var_name .
On the Windows taskbar, right-click the Windows icon and select System. In the Settings window, under Related Settings, click Advanced system settings. On the Advanced tab, click Environment Variables. Click New to create a new environment variable.
2.2 Set/Unset/Change an Environment Variable for the "Current" CMD Session. To set (or change) a environment variable, use command " set varname=value ". There shall be no spaces before and after the '=' sign. To unset an environment variable, use " set varname= ", i.e., set it to an empty string.
Environ and set default:With the environ dictionary variable value of the environment variable can be set by passing the key in the dictionary and assigning the value to it. With setdefault a default value can be assigned to the environment variable. Bypassing the key and the default value in the setdefault method.
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
sorry i should have made it clear,i tried row=str(row) but environment variableisnot set
It is set, but, as it would happen for any other language (bash
included), you are modifying environment variables of the current process, so it's normal that, if you come back to the shell, the change is not visible (it would be visible, instead, to processes started from your script, since environment variables are normally copied to child processes).
You cannot overcome this restriction; as explained in several other answers, it's not possible to set environment variables of the parent process. Period.
What people normally do is to source
a shell script to set the environment directly in the shell - the script runs under the current shell and thus modifies its environment variables. This of course can work only for the shell language.
Another alternative that some programs use (see e.g. ssh-agent
) is to print on the standard output a short shell script that sets the appropriate variables:
matteo@teokubuntu:~$ ssh-agent
SSH_AUTH_SOCK=/tmp/ssh-xkn6jGSZAxoA/agent.5184; export SSH_AUTH_SOCK;
SSH_AGENT_PID=5185; export SSH_AGENT_PID;
echo Agent pid 5185;
The program, then, is intended to be used as eval $(programname)
, which runs programname
's output in the current shell.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With