[英]Print into console terminal not into cell output of IPython Notebook
您必須將輸出重定向到系統標准輸出設備。 這取決於您的操作系統。 在 Mac 上,這將是:
import sys
sys.stdout = open('/dev/stdout', 'w')
在 IPython 單元格中鍵入上述代碼並對其進行評估。 之后所有輸出都將顯示在終端中。
在 Windows 上,這可以工作:
import sys
sys.stdout = open(1, 'w')
為了能夠輕松地從一種形式切換到另一種形式:
terminal_output = open('/dev/stdout', 'w')
print('this will show up in the IPython cell output')
print('this will show up in the terminal', file=terminal_output)
同樣,可以使用terminal_error = open('/dev/stderr', 'w')
發送到終端 stderr,不會與sys.stderr
的默認行為(即在 IPython 單元格中打印錯誤消息)發生任何沖突輸出)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.