[英]How can I get the last values from the environment variables with Python?
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. 我制作了一个批处理文件,然后设置了环境变量,并在Python脚本中对其进行了调用,但无法读取最后一个值。
I want an instruction or something in Python, that it brings the last values to me or to makes refresh for the values. 我想要Python中的指令或其他东西,它可以带给我最后的值或刷新这些值。
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 我没有为变量PCL_INC获得“ PCL \\ PCL-1.6.0 \\ include \\ pcl-1.6”
i get old value ` 我变老了
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). 每次启动新进程时,Windows都会为新进程(子进程) 复制启动进程(父进程)的环境表。 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 如果您的批处理文件(子进程)修改了环境变量,并且您想在Python脚本(父进程)中使用它们的值,则在批处理文件末尾需要类似
set >"%TEMP%\EnvValues.tmp"
which prints all environment variables with their values into file EnvValues.tmp
in directory for temporary files. 它将所有环境变量及其值打印到临时文件目录中的
EnvValues.tmp
文件中。 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. 然后,只要批处理文件未修改环境变量TEMP的值,就可以从Python脚本中加载该文件并提取所需的环境值。
You can use just set
if your Python script captures all output of the batch file written to stdout
. 如果您的Python脚本捕获了写入
stdout
的批处理文件的所有输出,则可以使用just set
。
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. 最后,如果您只对某些环境变量感兴趣,则还可以在批处理文件中使用
echo
来输出仅从stdout
捕获或重定向到临时文件的目标值,该值在批处理文件终止后由Python脚本读取。
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
: 用空格作为分隔符将变量的名称和值写入
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. 读取批处理文件输出的数据后,请不要忘记使用Python脚本删除临时文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.