IPython演示模式

时间:2010-02-08 18:59:21

标签: python ipython demo

我正在尝试使用IPython demo模式。我创建了一个名为test.py的文件,其中包含:

print 1
print 2
print 3

然后启动IPython并执行以下操作:

In [1]: from IPython.demo import LineDemo

In [2]: d = LineDemo('test.py')

In [3]: d()
********************* <test.py> block # 0 (5 remaining) *********************
p

********************************** output: **********************************
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)

/Users/tom/Library/Python/2.6/site-packages/ipython-0.10-py2.6.egg/IPython/demo.pyc in runlines(self, source)
    400         """Execute a string with one or more lines of code"""
    401 
--> 402         exec source in self.user_ns
    403 
    404     def __call__(self,index=None):

/Users/tom/tmp/<string> in <module>()
----> 1 
      2 
      3 
      4 
      5 

NameError: name 'p' is not defined

可能导致此错误的原因是什么?我错误地使用了LineDemo吗?

2 个答案:

答案 0 :(得分:2)

IPython中似乎有一个错误。在LineDemo.reload的demo.py中,该行显示:

src_b           = [l for l in self.fobj.readline() if l.strip()]

应该说:

src_b           = [l for l in self.fobj.readlines() if l.strip()]

目前它正在尝试执行第一行中的所有字母而不是文件中的所有行。

修改:Bug reported

答案 1 :(得分:0)

在IPython 0.9.1中工作正常 你有哪个版本?

In [1]: from IPython.demo import LineDemo

In [2]: d = LineDemo('test.py')

In [3]: d()
********************* <test.py> block # 0 (2 remaining) *********************
print 1
********************************** output: **********************************
1

In [4]: d()
********************* <test.py> block # 1 (1 remaining) *********************
print 2
********************************** output: **********************************
2

In [5]: d()
********************* <test.py> block # 2 (0 remaining) *********************
print 3
********************************** output: **********************************
3

******************************** END OF DEMO ********************************
******************** Use reset() if you want to rerun it. ********************