Sublime Text 3 Python 3 在 运行 时打印

Sublime Text 3 Python 3 print while running

在 Sublime Text 中 运行 编写脚本时,我无法让 Python 3 打印出任何内容。我可以在脚本完成后将其打印出来,但我需要它在运行时打印出来。

这是一些示例代码

import time

    for x in range(10):
        print("Test")
        time.sleep(1)

使用 Python 3 作为构建系统,我在 10 秒内什么也没得到,然后一次打印了 10 "Tests"。

如果我 运行 使用 Python 2 构建系统的相同脚本,我每秒打印 1 "Test",这就是我想要的。

同样,如果我 运行 在终端中使用 "python3 script.py" 的脚本,我每秒打印 1 "Test"。

谁能告诉我如何让 Sublime Text 3 中的构建系统在 运行s 时打印出 Python 3?

您必须打开 Python 3 构建系统配置文件,并确保将 -u 选项提供给要执行的命令行。

例如:

# ~/.config/sublime-text-3/Packages/User/Python3.sublime-build
{
    "cmd": ["/usr/bin/python3.5", "-u", "$file"],
    "selector": "source.python",
    "file_regex": "file \"(...*?)\", line ([0-9]+)"
}

引用 the documentation:

-u

Force the binary layer of the stdout and stderr streams (which is available as their buffer attribute) to be unbuffered. The text I/O layer will still be line-buffered if writing to the console, or block-buffered if redirected to a non-interactive file.

使用此选项,Python 脚本的输出将正确显示到 Sublime Text 中,而 运行。