通过 Powershell 脚本运行 python 脚本

Oam*_*otn 2 python powershell

我有一个 python 脚本,我可以使用以下代码通过 PowerShell 运行它:

cd User\PythonScripts
python TestFile.py
Run Code Online (Sandbox Code Playgroud)

现在我想通过 PowerShell 脚本(记事本文件并将其另存为 ps1文件)。

我用谷歌搜索了很多,但我找不到答案,但我认为它应该是这样的:

$path = 'C:\User\PythonScripts'
$file = 'TestFile.py
Run Code Online (Sandbox Code Playgroud)

我想我仍然想念对 python 的引用(所以它知道他需要哪个程序)。我需要怎么做?

Tom*_*Dee 6

假设 python 已经在你的路径变量中,你可以像这样调用一个 python 脚本:

python C:\User\PythonScripts\TestFile.py
Run Code Online (Sandbox Code Playgroud)


Sre*_*abr 6

我认为问题是您想使用 powershell 运行 python 脚本。

我认为下面的代码适合你

$path = 'C:\User\PythonScripts'
$file = 'TestFile.py'

$cmd = $path+"\\"+$file  # This line of code will create the concatenate the path and file
Start-Process $cmd  # This line will execute the cmd 
Run Code Online (Sandbox Code Playgroud)

将以上代码保存为.ps1并运行该 powershell 文件