13

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
Gabe
  • 84,912
  • 12
  • 139
  • 238
user3654069
  • 345
  • 2
  • 6
  • 14
  • 2
    So do `row = str(399)` - the compiler is telling you exactly what's wrong. Python is a dynamic but strongly typed language. – Benjamin Gruenbaum May 29 '14 at 00:16
  • 2
    Jeez have you even *read* the error message? What's unclear about that? – Matteo Italia May 29 '14 at 00:16
  • @MatteoItalia - sorry i should have made it clear,i tried row=str(row) but environment variableisnot set – user3654069 May 29 '14 at 00:24
  • That's a completely different problem. The environment variables are local to the current process (and typically inherited by children processes), which means that your code is just changing the local environment, which has no effect in the parent process (I suppose it's the shell) where you check the environment after the script execution. – Matteo Italia May 29 '14 at 00:26
  • @MatteoItalia - am running this script on a command line..does doing str(row) convert it to a different value,is there a way in can use set BUILD_VER=row using any call statement – user3654069 May 29 '14 at 00:33
  • @user3654069: not from Python. You can do it from a shell script, and only if you `source` it (not running it directly). – Matteo Italia May 29 '14 at 00:41
  • @MatteoItalia - its not on a unix machine,it is on a windows machine..basically i need to set this environment variable and there is a batch file which uses this to make a build?do you have any other suggestions? – user3654069 May 29 '14 at 00:48
  • This question has become a complete mess, please clarify in the body of the question, specifying in detail what is the actual problem you are trying to solve. In the meantime, I'm casting my close vote as "Unclear what you are asking". – Matteo Italia May 29 '14 at 00:48

2 Answers2

16

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
Gabe
  • 84,912
  • 12
  • 139
  • 238
  • sorry i tried that but the enviroment variable doesnt get set – user3654069 May 29 '14 at 00:23
  • is there a way in can use set BUILD_VER=row using any call statement – user3654069 May 29 '14 at 00:35
  • If you're trying to set environment variables from a shell script, you cannot use Python. You must do it from the shell script. – Gabe May 29 '14 at 00:41
  • 1
    I see from another comment that you are trying to do this from a batch file. You should have said this at the beginning. See my latest edit for how to do that. – Gabe May 29 '14 at 01:39
  • where are you using the row value?how does the batch know what is %v ?am trying to understand the link between %v and row? – user3654069 May 29 '14 at 02:03
  • The Python script prints the value of `row`. The `FOR /F` then parses what got printed out and puts it into `%v`, which is then used in the `SET` statement. – Gabe May 29 '14 at 03:02
  • are you sure what your said works for your,i tried what you suggested,i dont think the python script is executing neither the FOR /F parsing – user3654069 May 29 '14 at 04:22
  • @user3654069: If the Python script isn't executing, you have to figure out why. Maybe you need to call the Python interpreter directly (by invoking `Python.exe`). – Gabe May 29 '14 at 14:55
1

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.

Community
  • 1
  • 1
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • its not on unix machine,am trying on a windows machine,i have to use python to set it..based on the value that is set here,there a batch file that runs?do you have any other ideaS?i have tried to set using call("SET BUILD_VER={0}").format(row) but this is giving a compilaiton error – user3654069 May 29 '14 at 00:43
  • On Windows the same holds, with the additional restrictions that come from the fact that the shell is way more limited. Also, you cannot expect to make this work by throwing random stuff into the Python interpreter. – Matteo Italia May 29 '14 at 00:47
  • - request is simple.. i have a value that is in one of the variables of a python script..goal is to set that value as BUILD_VER as enviromnent variable...i was exploring if I can do it in the same python script...if it cant be done what are the options... – user3654069 May 29 '14 at 00:52