当通过批处理文件 运行 时,将 python 脚本的输出打印到 Windows 控制台

Printing output of python script to Windows console, when running via batch file

我试图查看 SO 上的其他资源,但 none 似乎解决了我的问题:

Executing Python Script From Command Line is Hiding Print Statements

How do I write output in same place on the console?

Python script doesn't print output in command prompt

我有一个通过批处理文件执行的 .py 脚本。在我的 .py 脚本中,我尝试了几次不同的尝试将输出打印到控制台 window。当我 运行 批处理文件和控制台 window 弹出时,我没有看到打印输出。我在执行程序时没有任何其他问题,另外我还使用 python 和批处理文件来生成文件输出等

这是我的 .py 脚本,我正在尝试根据其他 SO 文章编写输出的三种不同方式:

import sys

# Attempt 1
print('hello')

# Attempt 2
sys.stdout.write('hello')

# Attempt 3
if __name__ == '__main__':
    print('hello')

我的批处理文件:

"C:\Program Files\Anaconda3\pythonw.exe"  "\*********\printTest.py"
PAUSE

我的输出,none 我想要的 'hello' 输出显示。我已经编辑了脚本的位置,但目录已经过三重检查并且是正确的:

C:\Windows>"C:\Program Files\Anaconda3\pythonw.exe"  "\********\printTest.py"

C:\Windows>PAUSE
Press any key to continue . . .

如何让 'hello' 打印到控制台?

您是 运行 "pythonw.exe",它用于图形用户界面 (GUI) 脚本,而不是控制台脚本。 运行 "python.exe" 继承 CMD 的标准控制台 I/O。