【问题标题】:how to open a shell window output print real time debug information in python如何在python中打开shell窗口输出打印实时调试信息
【发布时间】:2013-12-17 14:04:32
【问题描述】:

我可以在python中打开一个shell窗口打印调试信息而不破坏代码的执行顺序吗?

例如:

def foo(b):
    print b

for a in range(0, 100):
    print a       # normally this will be used for on-the-fly simple debugging purpose 
                  # and will mess the console output of foo()          
    foo(a)

我可以这样做吗:

newshell = <new cmd.exe or bash>

for a in range(0, 100):
    newshell.print(a)       # information will be printed to new opened shell  
    foo(a)

提前致谢。

【问题讨论】:

  • 让我明白这一点吗?你想把控制台打开吗?一个有实际输出,另一个有错误,对吗?您使用的是什么操作系统?
  • @Paulo Bu,我想要一个跨平台的解决方案,win32 是首选,因为 *nix 会更容易一些,因为它有 /dev/tty vty pty ...但 win32 没有这样的设备.

标签: python shell debugging redirect


【解决方案1】:

你可以:

  1. 使用 python 日志记录。 http://docs.python.org/2/library/logging.html
  2. 只需写入文件并从终端执行:$ tail -f output.txt
  3. 写入你盒子里已经打开的终端(即linux),写入文件描述符/dev/pts/X(X:1,2...,你可以用$ who看到正确的数字)

【讨论】:

  • 谢谢,'logging' 可以将日志发送到另一个 shell 窗口吗?我不想涉及另一个临时文件。
  • 是的,可以。但这很奇怪。只需执行 logging.FileHandler('/dev/pts/X')。 X 是打开的终端的编号。
  • 我的只适用于 *nix 系统。 “pts”可能是“tty”,这取决于。查看命令 $ who。
  • 谢谢,它只适用于 *nix。我正在寻找一个支持 WIN32 和 *nix 的简单解决方案(可能命名为管道/套接字/子进程)
【解决方案2】:

我们不需要一个 shell 来打印一些东西。您可以使用Tkinter 文本widget

from Tkinter import Tk, Text, END

root = Tk()
text = Text(root)
text.pack()

def foo(b):
    print b

for a in range(0, 100):
    text.insert(END, str(a)+"\n")
    text.see(END)
    text.update()
    foo(a)

text.wait_window()  # wait until we close the window

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-12
    • 2011-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-22
    • 2010-11-15
    相关资源
    最近更新 更多