How do I print from a Python 2.7 script invoked from Bash within PyCharm?

混江龙づ霸主 提交于 2019-12-06 07:17:35
Martijn Pieters

Looks like you need to explicitly flush the buffer:

import sys

print 'foo'
sys.stdout.flush()
time.sleep(5)

print 'bar'
sys.stdout.flush()
time.sleep(5)

See Disable output buffering for Python 2 solutions that auto-flush after every print.

In your case, since you control the bash file that runs Python, just add -u or set PYTHONUNBUFFERED=1:

python -u test.py

or

PYTHONUNBUFFERED=1 python test.py

Just to add to @MartijnPieters' answer with regard to PyCharm:

In PyCharm, set the run configuration for the shell script under Run->Edit Configurations, like so:

Note the PYTHONUNBUFFERED=1.

You may have to first add a Bash run configuration under the Defaults menu on the left.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!