【问题标题】:Powershell prompt returning prompt but not Python output when trying to run a python program from Powershell尝试从 Powershell 运行 python 程序时,Powershell 提示返回提示但不返回 Python 输出
【发布时间】:2018-07-23 19:52:44
【问题描述】:

我正在尝试从 powershell 运行 python,我转到 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 目录中,其中包含一行:

打印“Hello World!”

我都试过了

python helloworld.py
.\python helloworld.py

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

【问题讨论】:

标签: python powershell


【解决方案1】:

您应该能够通过告诉 Powershell 在哪里找到 Python 来使其工作。 这可以通过设置环境变量来完成。

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

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

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

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

python --version

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

“Python 3.5.1”

【讨论】:

  • 我这样做了,现在 python 命令只返回提示,就像我之前键入 ./python 时一样。
  • 另外 python --version 也只是再次返回 powershell 提示符
  • 这听起来很奇怪。当 Powershell 刚刚返回到 promt 时,这意味着它已成功运行。也许你有一些奇怪的错误,或者你的 python 安装有些奇怪。你试过重启你的机器吗?否则我会建议重新安装 python。
  • 谢谢@Henrik Stanley Mortensen 重新安装 python 工作
【解决方案2】:

我自己还没有弄清楚,但作为一种解决方法,要么运行

cmd /C python helloworld.py

python helloworld.py COMMAND_LINE_ARGUMENTS

【讨论】:

    猜你喜欢
    • 2020-12-11
    • 2015-01-13
    • 2015-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-20
    • 1970-01-01
    • 2018-12-04
    相关资源
    最近更新 更多