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

时间:2022-02-04 05:50:43

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?

有人能指出我正确的方向来完成这项工作吗?

2 个解决方案

#1


0  

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

注意:仅当后者包含shell元字符(如空格)时,才需要在文件路径周围包含“...”。或者,在从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).
(From within PowerShell, simply execute what's inside "..." directly)

注意:这仅在从cmd.exe(批处理文件)调用时有效。 (从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'
  • &'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}
  • &{&'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.
(As an aside: your file path has an extraneous trailing space, but PowerShell seems to ignore that.)

由于您正在使用脚本块({...}),因此您将C:\ Users \ pc2 \ script.py作为参数传递,因此您必须在块内引用它,或者通过自动$ Args变量,或通过param()块中定义的参数。 (顺便说一下:你的文件路径有一个无关的尾随空间,但PowerShell似乎忽略了这一点。)

#2


0  

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. Save the file as something.bat :

在批处理中,您也可以使用相同的命令来执行python。将文件另存为something.bat:

python C:\Users\pc2\script.py

Hope it helps.

希望能帮助到你。

#1


0  

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

注意:仅当后者包含shell元字符(如空格)时,才需要在文件路径周围包含“...”。或者,在从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).
(From within PowerShell, simply execute what's inside "..." directly)

注意:这仅在从cmd.exe(批处理文件)调用时有效。 (从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'
  • &'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}
  • &{&'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.
(As an aside: your file path has an extraneous trailing space, but PowerShell seems to ignore that.)

由于您正在使用脚本块({...}),因此您将C:\ Users \ pc2 \ script.py作为参数传递,因此您必须在块内引用它,或者通过自动$ Args变量,或通过param()块中定义的参数。 (顺便说一下:你的文件路径有一个无关的尾随空间,但PowerShell似乎忽略了这一点。)

#2


0  

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. Save the file as something.bat :

在批处理中,您也可以使用相同的命令来执行python。将文件另存为something.bat:

python C:\Users\pc2\script.py

Hope it helps.

希望能帮助到你。