IPython 演示模式

2 投票
2 回答
561 浏览
提问于 2025-04-15 19:02

我正在尝试使用IPython的演示模式。我创建了一个叫做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

在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. ********************
2

在IPython里似乎有个小问题。在demo.py的LineDemo.reload里,有一行代码是:

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

应该改成:

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

现在它正在尝试执行第一行的所有字母,而不是文件里的所有行。

补充:这个问题已经被报告了

撰写回答