简体   繁体   English

从.bat文件运行python脚本并使用Powershell

[英]run python script from a .bat file and using Powershell

I run several python scripts on my W10 server and I'm looking for a way to open them all together. 我在W10服务器上运行了几个python脚本,我正在寻找一种将它们一起打开的方法。

I run manually my scripts opening a PowerShell on the script folder and executing pythonw script.py 我手动运行脚本,在脚本文件夹中打开PowerShell并执行pythonw script.py

I have tried several ways, with no result... 我尝试了几种方法,但没有结果...

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& 'C:\Users\pc2\AppData\Local\Programs\Python\Python36-32\pythonw.exe C:\Users\pc2\script.py'"

also tested: 还测试了:

powershell.exe -noexit -command "'pythonw C:\Users\pc2\script.py'"

And: 和:

powershell -command "& {&'C:\Users\pc2\AppData\Local\Programs\Python\Python36-32\pythonw.exe ' C:\Users\pc2\script.py}"

None of the above does anything... they should be opening the script.py file in using pythonw. 以上都不做任何事情...他们应该使用pythonw打开script.py文件。

Can anyone point me in the correct direction to get this done? 谁能指出我正确的方向来完成这项工作?

From the powershell console itself, you can directly run like this: 从powershell控制台本身,您可以像这样直接运行:

PS C:\Python27> python C:\Users\pc2\script.py

If necessary please edit the PATHTEXT environmental variable in the powershell profile: 如有必要,请在powershell配置文件中编辑PATHTEXT环境变量:

$env:PATHEXT += ";.py"

In the batch also you can put the same command to execute the python. 在批处理中,您还可以输入相同的命令来执行python。 Save the file as something.bat : 将文件另存为something.bat:

python C:\Users\pc2\script.py

Hope it helps. 希望能帮助到你。

  • To execute a single script: 要执行一个脚本:

     powershell -c "pythonw 'C:\\Users\\pc2\\script1.py'" 

Note: The enclosing '...' around the file path are only needed if the latter contains shell metacharacters such as whitespace. 注意:仅在文件路径包含外壳程序元字符(例如空格)时,才需要在文件路径周围加上'...'
Alternatively, use ""..."" when calling from cmd.exe (batch file), and `"...`" when calling from PowerShell. 另外,使用""...""从打电话时cmd.exe (批处理文件),以及`"...`"从PowerShell中调用时。

  • To execute multiple scripts, in sequence : 依次执行多个脚本:

     powershell -c "& { $Args | % { pythonw $_ } } 'C:\\Users\\pc2\\script1.py' 'C:\\Users\\pc2\\script2.py'" 

Note: This only works when calling from cmd.exe (batch file). 注意:仅当从cmd.exe (批处理文件)调用时,此方法才有效。
(From within PowerShell, simply execute what's inside "..." directly) (在PowerShell中,只需直接执行"..."内部的内容)


As for what you tried , focusing just on what command string PowerShell ended up seeing: 至于您尝试过的内容 ,仅关注PowerShell最终看到的命令字符串:

  • & 'C:\\Users\\pc2\\AppData\\Local\\Programs\\Python\\Python36-32\\pythonw.exe C:\\Users\\pc2\\script.py'

The problem is that you're passing the entire command line to & , whereas it only expects the executable name; 问题是您要将整个命令行传递给& ,而只需要可执行文件名;
C:\\Users\\pc2\\AppData\\Local\\Programs\\Python\\Python36-32\\pythonw.exe C:\\Users\\pc2\\script.py is obviously not a valid executable name / path. C:\\Users\\pc2\\AppData\\Local\\Programs\\Python\\Python36-32\\pythonw.exe C:\\Users\\pc2\\script.py显然不是有效的可执行文件名称/路径。

  • 'pythonw C:\\Users\\pc2\\script.py'

By virtue of being enclosed in '...' , this is a string literal , which PowerShell simply outputs as such - no command is ever executed. 凭借被封装在'...' ,这是一个字符串 ,它的PowerShell简单地输出作为这样-的要求不断不执行命令

  • & {&'C:\\Users\\pc2\\AppData\\Local\\Programs\\Python\\Python36-32\\pythonw.exe ' C:\\Users\\pc2\\script.py}

Since you're using a script block ( { ... } ), you're passing C:\\Users\\pc2\\script.py as an argument , so you'd have to refer to it as such inside the block, either via the automatic $Args variable, or via a parameter defined in a param() block. 由于您使用的是脚本块{ ... } ),因此您将C:\\Users\\pc2\\script.py作为参数传递,因此您必须在块内将其称为此类,通过自动$Args变量,或通过param()块中定义的param()
(As an aside: your file path has an extraneous trailing space, but PowerShell seems to ignore that.) (顺便说一句:您的文件路径具有多余的尾随空格,但是PowerShell似乎忽略了它。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM