执行 Python 文件时终端提示未更改为“>>>”

Terminal Prompt Not Changing To '>>>' When Executing Python File

我正在运行在终端上执行以下命令:

python SpellingCorrector.py

终端没有抛出任何错误,它只是前进到下一行,显示我当前工作目录的相同提示,而不是 Python '>>>' 终端提示。

我想 运行 程序中的一个带有参数的函数,我只能选择这样尝试:

[my/current/directory/]$ correction('speling')

这会抛出错误

bash: syntax error near unexpected token `'speling'`

我猜我需要 运行 它和这个提示一起工作:

>>> correction('speling')

Python版本为2.7.5。 有谁知道为什么当我 运行 程序或我如何 运行 函数时提示没有改变?

您需要在交互模式下执行您的脚本,如下所示:

python -i SpellingCorrector.py

然后从那里执行你的函数:

correction('speling')

您正在启动程序而不是启动 python 解释器。

要使用解释器,按如下方式启动它(不带参数):

python

然后使用import SpellingCorrector将您的程序导入解释器。 现在你可以使用它的功能等等

请注意,导入语句没有 .py 扩展名。

python script.py命令只是执行脚本和returns控制到shell,这不是你想要的。

您可以使用 ipython's %run 命令 运行 来自 interpeter 中的 python 脚本:

%run ./script.py

This is similar to running at a system prompt python file args, but with the advantage of giving you IPython’s tracebacks, and of loading all variables into your interactive namespace for further use.