Animations in ipython (jupyter) notebook - ValueError: I/O operation on closed file

Animations in ipython (jupyter) notebook - ValueError: I/O operation on closed file

我有一个生成动画的 jupyter notebook。它在我的旧笔记本电脑 (xubuntu gnu/linux) 上工作。现在在我的新笔记本电脑 (trisquel gnu/linux) 上它不工作了。这让我觉得我缺少一个库或类似的东西,但我无法解决它。

作为测试,我回到了最初学习如何制作 jupyter notebook 动画的教程 (http://louistiao.me/posts/notebooks/embedding-matplotlib-animations-in-jupyter-notebooks/)。当我 运行 编码单元格 [7].

时,我遇到了同样的问题

错误如下所示。

如能帮助解决此问题,我们将不胜感激。

非常感谢:)

[编辑 1] 如果我尝试 anim.save(...) 而不是 HTML(anim.to_html5_video()),我会遇到同样的问题。

[编辑 2] 如果我将笔记本下载为 *.py 文件并 运行 使用常规 python(而不是 ipython),那么我会遇到同样的问题.

[edit 3] 我的 ffmpeg 安装似乎可以正常工作,因为我能够从一系列图像手动创建动画。我现在可以使用它作为解决方法,但它非常笨重, HTML(anim.to_html5_video()) 解决方案应该可以工作,所以仍然需要排序。

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-5114ccf53b4c> in <module>()
----> 1 HTML(anim.to_html5_video())

/usr/local/lib/python2.7/site-packages/matplotlib/animation.pyc in to_html5_video(self)
   1157                                 bitrate=rcParams['animation.bitrate'],
   1158                                 fps=1000. / self._interval)
-> 1159                 self.save(f.name, writer=writer)
   1160 
   1161             # Now open and base64 encode

/usr/local/lib/python2.7/site-packages/matplotlib/animation.pyc in save(self, filename, writer, fps, dpi, codec, bitrate, extra_args, metadata, extra_anim, savefig_kwargs)
   1011                         # TODO: See if turning off blit is really necessary
   1012                         anim._draw_next_frame(d, blit=False)
-> 1013                     writer.grab_frame(**savefig_kwargs)
   1014 
   1015         # Reconnect signal for first draw if necessary

/usr/local/lib/python2.7/contextlib.pyc in __exit__(self, type, value, traceback)
     33                 value = type()
     34             try:
---> 35                 self.gen.throw(type, value, traceback)
     36                 raise RuntimeError("generator didn't stop after throw()")
     37             except StopIteration, exc:

/usr/local/lib/python2.7/site-packages/matplotlib/animation.pyc in saving(self, *args, **kw)
    254             yield self
    255         finally:
--> 256             self.finish()
    257 
    258     def _run(self):

/usr/local/lib/python2.7/site-packages/matplotlib/animation.pyc in finish(self)
    274     def finish(self):
    275         'Finish any processing for writing the movie.'
--> 276         self.cleanup()
    277 
    278     def grab_frame(self, **savefig_kwargs):

/usr/local/lib/python2.7/site-packages/matplotlib/animation.pyc in cleanup(self)
    309     def cleanup(self):
    310         'Clean-up and collect the process used to write the movie file.'
--> 311         out, err = self._proc.communicate()
    312         self._frame_sink().close()
    313         verbose.report('MovieWriter -- '

/usr/local/lib/python2.7/site-packages/subprocess32.pyc in communicate(self, input, timeout)
    925 
    926         try:
--> 927             stdout, stderr = self._communicate(input, endtime, timeout)
    928         finally:
    929             self._communication_started = True

/usr/local/lib/python2.7/site-packages/subprocess32.pyc in _communicate(self, input, endtime, orig_timeout)
   1711             if _has_poll:
   1712                 stdout, stderr = self._communicate_with_poll(input, endtime,
-> 1713                                                              orig_timeout)
   1714             else:
   1715                 stdout, stderr = self._communicate_with_select(input, endtime,

/usr/local/lib/python2.7/site-packages/subprocess32.pyc in _communicate_with_poll(self, input, endtime, orig_timeout)
   1767             select_POLLIN_POLLPRI = select.POLLIN | select.POLLPRI
   1768             if self.stdout:
-> 1769                 register_and_append(self.stdout, select_POLLIN_POLLPRI)
   1770                 stdout = self._fd2output[self.stdout.fileno()]
   1771             if self.stderr:

/usr/local/lib/python2.7/site-packages/subprocess32.pyc in register_and_append(file_obj, eventmask)
   1746             poller = select.poll()
   1747             def register_and_append(file_obj, eventmask):
-> 1748                 poller.register(file_obj.fileno(), eventmask)
   1749                 self._fd2file[file_obj.fileno()] = file_obj
   1750 

ValueError: I/O operation on closed file

我 运行 遇到了同样的问题(在 mac os 10.12.2 上),并通过重新安装 ffmpeg 并启用 x264 编解码器来修复它,这to_html5_video() 函数需要将动画转换为 html5。

要安装 x264:

cd /my/path/where/i/keep/compiled/stuff
git clone git://git.videolan.org/x264.git
cd x264
./configure --enable-static --enable-shared
make
make install
ldconfig # note: use for linux, not necessary for mac

source

要在启用 x264 编解码器的情况下安装 ffmpeg:

cd /my/path/where/i/keep/compiled/stuff
git clone http://source.ffmpeg.org/git/ffmpeg.git
cd ffmpeg
./configure --enable-gpl --enable-libx264
make
make install
ldconfig # note: use for linux, not necessary for mac

source