在 IPython 中使用输入重定向命令(“<”)

Using input redirection command ("<") in IPython

在windows 命令行中,我使用redirection operator ("<") 从文件读取输入。对于 运行 我的 python 模块,我会做类似的事情:

python myscript.py <input.txt

myscript.py 中,我使用 sys.stdin.readline() 读取输入流,类似于:

def main(argv):
    line = sys.stdin.readline() 
    while line:
        doSomething()
        line = sys.stdin.readline()

if __name__ == "__main__": 
    main(sys.argv)

这可以在命令行上找到。

有没有办法在IPython中使用重定向命令?我想调试文件。谢谢。

我正在 运行ning Python 3.5.1:: Anaconda 2.5.0 on Win64。

不容易。重定向是主机命令 shell 的一项功能,并且 运行 在 IPython 中设置任何内容都会将您与它隔离。

另一种完成您正在寻找的方法是将 IPython 带入您的程序。如果知道断线的地方,可以在断线附近的 try-except 的 except 块中添加以下代码:

import IPython
IPython.embed()

这将在发生错误的上下文中启动交互式 IPython shell。

或者,您可以 运行 调试器控制下的程序: Step-by-step debugging with IPython