如何在Python脚本中使用的值设置环境变量 [英] How to use a value in a python script to setup an environment variable

查看:594
本文介绍了如何在Python脚本中使用的值设置环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题设立一个环境变量,又看了看其他的例子集在python脚本环境变量,一是制定

1.我有一个python脚本变量版本的值。
2.Now,我需要为BUILD_VER在Windows这是环境变量(目前它在Windows中,preFER,以使其在OS通用),通常是一组简单的BUILD_VER =%版本%在批处理文件会做(这个环境变量被另一个批处理文件,这使得软件构建我的猜测是无关这里)

现在,未来和聚焦的问题。

有没有一种方法我可以设置BUILD_VER =版本在同一个python脚本并把它传递到一个批处理文件?任何其他的想法?我想在一个批处理文件下面,但我不知道如果python脚本运行?运行以下犯规输出任何东西。

 关闭@echo
在/ F%V('build_ver.py 123637')DO SET BUILD_VER =%V


解决方案

您还没有明确规定的多个批处理文件和Python脚本的相互关系,所以让我们分析一些的可能性:


  • Python脚本定义变量,执行该设置同一个变量的批处理文件,然后将Python脚本的本身继续的并执行您想要访问的变量其他批处理文件在第一批文件中定义。这仅仅是不可能的。期。


  • Python脚本定义变量,执行该设置相同的变量,然后批处理文件的批处理文件继续的并执行继承变量其他批处理文件。完全有效的。在这种情况下,python脚本只是用来定义变量,并启动了第一批文件。


  • 第一个批处理文件执行开始,执行python脚本只是为了让变量的值,它定义和执行继承变量其他批处理文件。这是最简单的解决方案;在这种情况下,Python脚本仅用于获取变量的值


实施最后解决的方式就像是你的榜样:

 关闭@echo
REM执行Python脚本传递给它这个批处理文件的第一个参数
在('build_ver.py%1')/ F %% v请勿SET BUILD_VER = %% v
由python脚本定义回声值:%BUILD_VER%REM调用继承这个变量另一个批处理文件:
调用AnotherBatch.bat

请注意,在你的code你错过了一对夫妇在FOR / F命令百分号...

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

解决方案

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...

这篇关于如何在Python脚本中使用的值设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆