通过 Powershell 脚本运行 python 脚本

新手上路,请多包涵

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

 cd User\PythonScripts
python TestFile.py

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

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

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

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

原文由 Oamriotn 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.4k
2 个回答

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

 python C:\User\PythonScripts\TestFile.py

原文由 Tom Dee 发布,翻译遵循 CC BY-SA 4.0 许可协议

我认为问题是您想使用 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文件

原文由 Sreevathsabr 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题