2

I am having the following issue setting up an enviroment variable,looked at other examples set environment variable in python script ,one worked

1.I have a value in variable "version" in a python script. 2.Now,I need to set this as enviroment variable for BUILD_VER in windows(currently its in windoWs,prefer to make it generic across OS),normally a simple SET BUILD_VER=%version% in a batch file would do(this enviroment variable is used by another batchfile which makes a software build which I guess is unrelated here)

Now,coming and focusing to the problem.

Is there a way I can set BUILD_VER =VERSION in the same python script and pass it onto a batch file?any other ideas?i tried the following in a batch file but am not sure if the python script is running ?running the following doesnt output anything

@echo off
FOR /F %v IN ('build_ver.py 123637') DO SET BUILD_VER=%v
Community
  • 1
  • 1
user3654069
  • 345
  • 2
  • 6
  • 14
  • possible duplicate of [Add an environment variable using Python](http://stackoverflow.com/questions/17331789/add-an-environment-variable-using-python) – Nir Alfasi May 29 '14 at 03:38
  • Maybe this can help: http://code.activestate.com/recipes/416087-persistent-environment-variables-on-windows/ – Nir Alfasi May 29 '14 at 03:40
  • @alfasin - please unlock it,i looked at the question,that wont work,please read again before jumping to conclusions – user3654069 May 29 '14 at 03:41
  • @alfasin - can you reopen this?closed it pointing to some link which doesnt work – user3654069 May 29 '14 at 03:57
  • If you want this to be cross-platform, well, no code will ever work. The correct answer that linux environment variables can not be set in parent processes has already been given long ago... Not sure about Windows. Maybe tag it windows and change title slightly? – Paul May 29 '14 at 04:37
  • @paul - thats fine,lets not sidetrack,can we atleast get it to work on windows? – user3654069 May 29 '14 at 04:57
  • Sorry, I don't do windows here. – Paul May 29 '14 at 04:58
  • It seems, though, that you are trying to create a setting in a windows batch file, where the setting comes from running a program. Perhaps if you frame your question that way you can find or get an answer. – Paul May 29 '14 at 05:00

1 Answers1

2

You have not clearly specified the inter-relation of the several batch files and your python script, so lets analyze some of the possibilities:

  • The python script define the variable, execute a batch file that set the same variable and then the python script continue by itself and execute other batch files that you want have access to the variable defined in the first batch file. This is just not possible. Period.

  • The python script define the variable, execute the batch file that set the same variable and then the batch file continue and execute other batch files that inherits the variable. Perfectly valid. In this case the python script is just used to define the variable and start the first batch file.

  • The first batch file start execution, execute the python script just to get the value of the variable, define it and execute other batch files that inherits the variable. This is the simplest solution; in this case the python script is used only to get the value of the variable.

The way to implement the last solution is like in your example:

@echo off
rem Execute python script passing to it the first parameter of this Batch file
FOR /F %%v IN ('build_ver.py %1') DO SET BUILD_VER=%%v
echo Value defined by python script: %BUILD_VER%

rem Call another Batch file that inherits this variable:
call AnotherBatch.bat

Note that in your code you missed a couple of percent signs in the FOR /F command...

Aacini
  • 65,180
  • 12
  • 72
  • 108