IPython演示模式

发布于 2024-08-21 06:05:04 字数 1157 浏览 8 评论 0原文

我正在尝试使用 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?

I'm trying to use the IPython demo mode. I created a file called test.py containing:

print 1
print 2
print 3

and then launched IPython and did the following:

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

What is likely to be causing this error? Am I using LineDemo incorrectly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七秒鱼° 2024-08-28 06:05:04

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()]

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

编辑:报告的错误

There seems to be a bug in IPython. In demo.py in LineDemo.reload, the line that says:

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

should say:

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

Currently it's trying to execute all the letters in the first line instead of all the lines in the file.

Edit: Bug reported.

唯憾梦倾城 2024-08-28 06:05:04

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

It works ok in IPython 0.9.1
Which version do you have?

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. ********************
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文