可能重复:
如何刷新Python打印输出?
我有一个运行的算法,需要一段时间,所以我想通过打印到控制台来跟踪它通过了多远.
所以类似于:
import sys
def myMethod():
i = 0
while (i<1000000):
i = i+1
output_str = str(i) + "\n"
sys.stdout.write(output_str) # same as print
sys.stdout.flush()
myMethod()
Run Code Online (Sandbox Code Playgroud)
如何在运行时打印此打印件,而不是在最后?
编辑,解决方案: - 发布修改后的代码.当你在linux终端上运行它时,这段代码工作正常
python filename.py
Run Code Online (Sandbox Code Playgroud)
但是当我在Wing 101 IDE中运行它时 - 按下绿色播放按钮('运行python shell中编辑器的内容') - 它等待程序完成后再输出.
我是Python的新手,并且一直在使用Wing IDE来使用这些功能.我在环顾四周时可以找到的一件事是在执行一个不会很快终止的命令时如何强制终止Python shell.一个例子是:
import math
math.factorial(1000000)
Run Code Online (Sandbox Code Playgroud)
我知道在Visual Studio C++中,命令是Ctrl + C,但Python究竟是什么呢?
我一直在使用Wing IDE进行python编程,我正在尝试切换到Eclipse,PyDev.
当我在Wing IDE中运行我的代码时,在完成执行后,控制台会立即回到交互式shell,我可以继续测试,但我不知道如何在Eclipse中执行此操作.我不确定我是否正确描述了我的问题,所以我将使用一个例子:
假设我有一个看起来像这样的简单源代码(例如test.py):
print("hello")
Run Code Online (Sandbox Code Playgroud)
当我通过单击绿色箭头在Wing IDE中运行它时,控制台在执行后将如下所示:
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)]
Type "help", "copyright", "credits" or "license" for more information.
[evaluate untitled-1.py]
hello
>>>>
Run Code Online (Sandbox Code Playgroud)
我可以继续在shell上做任何事情,它会知道我的代码(定义函数等).但是当我在Eclipse中做同样的事情时,控制台看起来就像这样:
hello
Run Code Online (Sandbox Code Playgroud)
然后我必须单击"删除所有已终止的启动"按钮返回到shell.
这可以在Eclipse中完成吗?