为什么在 powershell 中无法识别“python”?

新手上路,请多包涵

我在我的 Windows 10 PC 上安装了 python 3.6。我还使用 Pycharm 来制作我的脚本。它在那里运行得很好。但我的问题是我无法在 powershell 中运行 python。我被告知只需在 powershell 中键入“python”,它就会自动启动 python。但是,我收到一条错误消息,说它无法识别。

 Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

PS C:\Users\lenovo> python
python : The term 'python' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ python
+ ~~~~~~
    + CategoryInfo          : ObjectNotFound: (python:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

有人建议输入:

 [Environment]::SetEnvironmentVariable("Path", "$enc:Path;C:\Users\lenovo\AppData\Local\Programs\Python\Python36")

上面代码中给出的路径对我的电脑来说是正确的。事实上,Python 在我给出上面的代码后运行。但是,那是我的问题。每次我关闭并再次打开 powershell 时,我都必须输入上面的代码才能让 python 运行。我也尝试在以管理员身份运行的 Powershell 中键入此内容。但是,它不起作用。我什至尝试重新启动我的电脑,问题仍然无法解决。

每次我想在新的 powershell 窗口中运行 python 时,我都需要输入上面的代码。这有点烦人。任何帮助,将不胜感激。谢谢。

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

阅读 2.6k
2 个回答

您需要将路径添加到环境变量:

控制面板>系统和安全>系统>高级系统设置>环境变量

选择编辑 PATH 并 附加

C:\Users\lenovo\AppData\Local\Programs\Python\Python36

到路径。点击应用并确定。关闭并重新打开 shell 后,您应该能够键入“python”并进入 python shell。

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

或者只需在 PowerShell 中键入“py”而不是“python”,然后按回车键。

查看图像以查看其工作原理。

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

推荐问题