首页 > 解决方案 > 尝试从 Powershell 运行 python 程序时,Powershell 提示返回提示但不返回 Python 输出

问题描述

我正在尝试从 powershell 运行 python,我使用

cd f:\python27

如果我输入 python 我得到以下错误(尽管它是文件夹中的 exe 文件):

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
Suggestion [3,General]: The command python was not found, but does exist in the 
current location. Windows PowerShell does not load commands from the current 
location by default. If you trust this command, instead type: ".\python". See "get-
help about_Command_Precedence" for more details.

如果我输入 .\python 它什么也不做,并再次返回提示。我在文本编辑器中创建了一个名为 helloworld.py 的简单文件,并将其保存到 python27 目录中,其中包含一行:

打印“你好世界!”

我试着同时输入

python helloworld.py
.\python helloworld.py

第一个返回原始错误,第二个再次返回提示,显然没有做任何事情,我无法让 python 程序运行。我是 Powershell 和 Pyton 的新手,我正在努力学习 python,任何人都可以帮助或指出我哪里出错并解释我做错了什么以及解决方案是什么?谢谢

标签: pythonpowershell

解决方案


你应该可以通过告诉 Powershell 在哪里找到 Python 来让它工作。这可以通过设置环境变量来完成。

以管理员身份打开 Powershell。运行此代码:

[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Program Files\Python35-32")

注意:在这里,在路径中,添加您在系统中安装 Python 的路径,在我的例子中是“C:\Program Files\Python35-32”。

这应该有助于解决这个问题。
对他们来说,尝试运行:

python --version

它应该显示系统中安装的 Python 的版本号,这就是它在我的系统中显示的内容:

“Python 3.5.1”</p>


推荐阅读