繁体   English   中英

从任何地方运行 Python 脚本

[英]Running Python Scripts from Anywhere

我是编程新手。 再试一次。

我已经能够通过更改环境变量使 Python 从 PowerShell 运行。 当我尝试使用 Win + R 启动 Python 时,只有 py 会启动 Python。 这让我很困惑。 PowerShell 和 cmd 只输入python没有问题。

我也在努力做到这一点,以便我可以从任何地方启动 python 脚本。 我在 Visual Studio 代码中创建了一个名为 again.py 的简单脚本,并将脚本保存在特定文件夹中。 当我尝试在 VSC 中运行脚本时,总是出现错误:

[Running] python -u "c:\\Users\\xyz\\Programming\\Python\\again.py" 'python' is not recognized as an internal or external command, operable program or batch file.

[Done] exited with code=1 in 0.137 seconds

据我了解,如果一个文件夹包含在 Path 中,并且调用了一个存储在该文件夹中的文件,则该文件/脚本应该运行。 我应该能够从任何地方调用该文件,但我无法在 PowerShell 或 Run 中键入文件名并让它运行。

win + R 返回:

Windows cannot find 'again.py'. Make sure you type the name correctly, and try again.

PowerShell:重新启动.py

start : This command cannot be run due to the error: The system cannot find the file specified. At line:1 char:1 + start again.py + ~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

在 cmd 中输入 again.py 使其运行得很好。

Powershell 错误是由文件名中的空格引起的。 Powershell 查看输入, start again.py ,然后像这样解析它

first token on row is word "start"
    start is an alias to start-process
second token on row is "again.py"
    that will be passed as argument to start-process

最简单的解决方法是避免文件名中出现空格。 请改用下划线或驼峰式大小写。 也就是说, startAgain.pystart_again.py 可以使用空格,但涉及额外的引用技巧。 更糟糕的是,那些依赖于shell。 Cmd 有自己的规则,Powershell 有非常不同的规则。 由于您正在学习如何编程,请考虑暂时关注 Python,稍后再考虑 shell 引用问题。

暂无
暂无

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

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