通过Powershell scrip运行python脚本

2024-05-20 00:00:58 发布

您现在位置:Python中文网/ 问答频道 /正文

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

cd User\PythonScripts
python TestFile.py

现在我想通过PowerShell脚本(记事本文件并将其保存为ps1文件)运行这些简单的命令。

我搜索了很多,但找不到答案,但我想应该是这样的:

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

我想我还是没有提到python(所以它知道他需要哪个程序)。我需要怎么做?


Tags: 文件path答案代码py命令脚本cd
3条回答

我想问题是你想用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 

将上述代码另存为.ps1,并运行powershell文件

假设python已经在您的路径变量中,您可以调用如下python脚本:

python C:\User\PythonScripts\TestFile.py

我认为下面的代码可以工作。

cd C:\用户\ PythonScripts

.\python测试文件.py

如果python.exe是替代python的可执行文件,请提及它。 将以上代码保存在.ps1文件中并在powershell上运行

相关问题 更多 >