0

I made a batch file, that then sets the environment variables, and I call it in a Python script, but I couldn't read the last values.

I want an instruction or something in Python, that it brings the last values to me or to makes refresh for the values.

setx PCL_INC %cd%\PCL\PCL-1.6.0\include\pcl-1.6

python

os.system("F:\\Labeleditor\\build-toolset\\scripts\\setenv_VS2008_64bit_leV4") self.gui.lineEdit_2.setText(os.environ['PCL_INC'])

i don't get "PCL\PCL-1.6.0\include\pcl-1.6" for the variable PCL_INC

i get old value `

  • 2
    Please can you post your code? – br3w5 Oct 23 '14 at 15:39
  • setx ADTF_DIR %ADTF_DIR% setx ADTF_ADDONS "%ADTF_DIR%\addons" setx QTDIR "%cd%\Qt\qt-4.7.1_win32_vc90" setx OSG_DIR "%cd%\osg\osg-3.2.0-win32-vc90" – Malaz Albawarshi Oct 23 '14 at 15:58
  • os.environ['ADTF_ADDONS'], to read the value – Malaz Albawarshi Oct 23 '14 at 15:59
  • Do you change the environment variables from outside the script (while it's running)? – moooeeeep Oct 23 '14 at 16:03
  • `setx` sets the variable for future cmd-processes, not for the running process itself. Use `set` instead, which sets the variable for the running process, but not for future processes. – Stephan Oct 23 '14 at 16:27
  • If you want that the python program execute a batch file that set the variable and then the python script continue and get the value defined in the first batch file, it is not possible. See: http://stackoverflow.com/questions/23925253/how-to-use-a-value-in-a-python-script-to-setup-an-environment-variable/23925971#23925971 – Aacini Oct 23 '14 at 17:02
  • @Malaz Albawarshi: When you are asked to post code you are supposed to edit your question, not putting it in comments. [This is not a forum](http://meta.stackexchange.com/a/92115). – Peter Mortensen Oct 23 '14 at 21:14
  • Those 2 code blocks can't be your real code as the batch file does not write anything to a file and the Python script does not read anything from file. And for some unknown reason you are still using `setx` instead of `set`. Do you have still not read the documentation for those two commands and therefore still do not know the difference? It looks like you are wasting our time as ignoring all advices. Also cleanup this question be deleting your comments not helpful for anybody. An X symbol appears at end of a comment if mouse pointer is hovered over one or your comments. – Mofi Oct 30 '14 at 18:52
  • no i know the difference!!, in python script i run batch file called "setenv_VS2008_64bit_leV4" to set new values to the Enviroment variables, and i can't use set because i lose the value when the i come back to the python script and i use `self.gui.lineEdit_2.setText` to see the value of the variable from python i try before `set` before, it works when i work in the same cmd – Malaz Albawarshi Oct 31 '14 at 10:37
  • actually i need to update the environment variables without close cmd, i faced this problem at my first days at work, that's why i couldn't explain it very well sorry – Malaz Albawarshi Oct 31 '14 at 10:55

1 Answers1

1

Every time a new process is started, Windows makes a copy of the environment table of the starting process (parent process) for the new process (child process). The child process - the batch file in your case - can modify its environment table. And all processes started by this child process get a copy of current table. But it is not possible to manipulate from a child process the environment table of the parent process. There is no way to do this.

If your batch file (child process) modifies environment variables and you want their values in your Python script (parent process), you need at end of your batch file something like

set >"%TEMP%\EnvValues.tmp"

which prints all environment variables with their values into file EnvValues.tmp in directory for temporary files. You can then load this file from within your Python script and extract the environment values you want as long as value of environment variable TEMP was not modified by the batch file.

You can use just set if your Python script captures all output of the batch file written to stdout.

Last if you are interested in only some environment variables, you can also use echo in the batch file to output just the values of interest either captured from stdout or redirected to a temporary file which is read in by the Python script after batch file terminated.

Example:

Write names and values of the variables with equal sign as separator to file:

@echo off
echo ADTF_DIR=%ADTF_DIR%>"%TEMP%\EnvValues.tmp"
echo ADTF_ADDONS=%ADTF_DIR%\addons>>"%TEMP%\EnvValues.tmp"

Write just the values to file:

@echo off
echo %ADTF_DIR%>"%TEMP%\EnvValues.tmp"
echo %ADTF_DIR%\addons>>"%TEMP%\EnvValues.tmp"

Write names and values of the variables with space as separator to stdout:

@echo off
echo ADTF_DIR %ADTF_DIR%
echo ADTF_ADDONS %ADTF_DIR%\addons

Don't forget to delete the temporary file with Python script after reading in the data output by the batch file.

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • for the second solution, you mean something like that ? _italic_ **bold**` setx ADTF_DIR %ADTF_DIR% setx ADTF_ADDONS "%ADTF_DIR%\addons" echo %ADTF_DIR% echo %ADTF_ADDONS% ` – Malaz Albawarshi Oct 24 '14 at 12:10
  • See edited answer above. Do you already know what [setx](http://technet.microsoft.com/en-us/library/cc755104.aspx) is for? `setx` is for modification of the environment variables in Windows registry. That are the environment variables shown under **Control Panel - System - Advanced System Settings** after clicking on button **Environment Variables**. Do you really want to set variables persistent for current user or entire machine (with administrator privileges)? – Mofi Oct 24 '14 at 12:45
  • i try to set values by Batchfile,and i want to get the value from python and i call the batchfile from python script, but the problem is i get the old values. – Malaz Albawarshi Oct 30 '14 at 16:18
  • `setx PCL_INC %cd%\PCL\PCL-1.6.0\include\pcl-1.6` python `os.system("F:\\Labeleditor\\build-toolset\\scripts\\setenv_VS2008_64bit_leV4") self.gui.lineEdit_2.setText(os.environ['PCL_INC']) ` – Malaz Albawarshi Oct 30 '14 at 17:36
  • i don't get "PCL\PCL-1.6.0\include\pcl-1.6" for the variable PCL_INC i get the old value – Malaz Albawarshi Oct 30 '14 at 17:39