UnsupportedOperation:fileno - 如何解决这个Python依赖性混乱?

时间:2015-10-22 15:55:33

标签: python python-imaging-library pillow

我正在构建相当广泛的Python后端,并且在服务器A上工作得很好。然后我将系统安装在新的(开发)服务器B上,我只是从头开始再次安装所有pip包。事情看似很好,所以我做了pip freeze。然后,我接受了该列表并升级了服务器A上的软件包。

但是,正如你所料,我应该知道的更好。我没有在机器B上测试过足够的东西,所以我遇到了Pillow 3.0.0版本的问题。所以我降级到版本1.7.8。这解决了这个问题,bug给了我另一个问题:

File "/home/kramer65/theproject/app/models/FilterResult.py", line 26, in to_json
    self.image.save(b, 'JPEG')
  File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 1437, in save
    save_handler(self, fp, filename)
  File "/usr/local/lib/python2.7/dist-packages/PIL/JpegImagePlugin.py", line 471, in _save
    ImageFile._save(im, fp, [("jpeg", (0,0)+im.size, 0, rawmode)])
  File "/usr/local/lib/python2.7/dist-packages/PIL/ImageFile.py", line 476, in _save
    fh = fp.fileno()
UnsupportedOperation: fileno

在这里,我有点失落。据我所知,这是Pillow本身存在的一个问题,所以我不知道它为什么会起作用以及为什么它不再起作用了。

我在互联网上搜索过,但我找不到任何解决方案。

有人知道我能做些什么来解决这个问题吗?

PS。未安装PIL,因此它不是PIL和Pillow之间的碰撞

[编辑]

我刚在Python中测试了一个import Image(这表明仍然安装了PIL)。令我惊讶的是成功,即使pip告诉我它没有安装:

$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import Image
>>> exit()
$ sudo pip uninstall PIL
The directory '/home/hielke/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Cannot uninstall requirement PIL, not installed

2 个答案:

答案 0 :(得分:3)

BytesIO方法被调用时,

UnsupportedOperation个对象引发AttributeError(而不是StringIO fileno这样做的异常未被处理为应该是。

通过捕获异常,此提交https://github.com/python-pillow/Pillow/commit/197885164b22f82653af514e66c76f4b778c0b1b在Pillow 3.0.0中修复了此问题。以下是修复。该提交的其余部分是对测试套件的更改。

PIL/ImageFile.py

 @@ -29,6 +29,7 @@

 import Image
 import traceback, os
+import io

 MAXBLOCK = 65536

 @@ -475,7 +476,7 @@ def _save(im, fp, tile):
     try:
         fh = fp.fileno()
         fp.flush()
-    except AttributeError:
+    except (AttributeError, io.UnsupportedOperation):
         # compress to Python file-compatible object
         for e, b, o, a in tile:
             e = Image._getencoder(im.mode, e, a, im.encoderconfig)

你可以简单地修补1.7.8来处理异常。

答案 1 :(得分:1)

我终于设法解决了问题。我将枕头从3.0.0降级到1.7.8的原因是因为我在Pillow Pypi package index上看到的仅有两个版本。我终于想起我还有一台服务器,我曾经测试过这个代码,并且它仍在工作。快速pip freeze告诉我它安装了Pillow版本2.3.0。因此,在我的开发服务器上安装后,事情再次发挥得非常好。

那么我从中学到了什么?使用pip freeze