【问题标题】:AttributeError: 'cStringIO.StringO' object has no attribute 'fileno' when using django-imagekitAttributeError: 'cStringIO.StringO' 对象在使用 django-imagekit 时没有属性 'fileno'
【发布时间】:2015-04-16 12:44:20
【问题描述】:

我使用django-imagekit处理上传图片,遇到如下错误:

AttributeError at /car/7/

'cStringIO.StringO' object has no attribute 'fileno'

Request Method:     GET 
Request URL:    http://luxingnan.azurewebsites.net/car/7/
Django Version:     1.8
Exception Type:     AttributeError
Exception Value:    

'cStringIO.StringO' object has no attribute 'fileno'

Exception Location:     D:\home\site\wwwroot\env\Lib\site-packages\pilkit\utils.py in
__enter__, line 248
Python Executable:  D:\Python27\python.exe
Python Version:     2.7.8
Python Path:    

[u'D:\\home\\site\\wwwroot\\env\\Lib\\site-packages',  '.',  'D:\\Windows\\SYSTEM32\\python27.zip',  'D:\\Python27\\DLLs',  'D:\\Python27\\lib',  'D:\\Python27\\lib\\plat-win',  'D:\\Python27\\lib\\lib-tk',  'D:\\Python27',  'D:\\Python27\\lib\\site-packages',  'D:\\home\\site\\wwwroot']

Server time:    Thu, 16 Apr 2015 12:28:26 +0000

下面是我的代码:

# models.py
class Carpic(models.Model):
    picture = models.ImageField('pic',upload_to='car-pictures')
    picture_slide = ImageSpecField(source='picture',
        processors=[ResizeToFill(762, 456)],
        format='JPEG',
        options={'quality': 60}
        )
# template.html
{% for pic in pictures %}
<li><img src="{{pic.picture_slide.url}}"/></li>
{% endfor %}

谁能告诉我该怎么做?谢谢

【问题讨论】:

  • 这个错误与pilkit图像处理包有关,我跟踪了一些研究发现问题与pilkit/utils.py中的FileWrapper类试图调用fileno()有关一个 StringIO 实例,这个 SO 答案提供了详细的解释 stackoverflow.com/a/5903627/4724196
  • 尝试在这里github.com/matthewwithanm/pilkit/issuesgithub.com/matthewwithanm/pilkit/issues在 pilkit 的 repo 中打开一个问题
  • 奇怪的是在本地环境中一切都很好。但是在我将它部署到 Azure 之后,它给了我这个错误
  • 你是在linux环境下开发还是在windows上开发?
  • @HassenPy 谢谢哈森。相反,我尝试了 Heroku,现在一切正常。我想这可能是 Azure 的问题

标签: python django django-imagekit


【解决方案1】:

刚刚有机会看看这个(和your GH Issue)。我将在此处包含我的回复,因为这似乎是明智的做法(:

所以看起来这是 Azure 的一个怪癖,但我们绝对可以在 PILKit 中修复它。

PILKit 有 a utility 用于消除 PIL 的一些噪音。它这样做的方式是temporarily replacing stderr(使用它的文件描述符)。显然在 Azure 上,stderr 是 StringIO 的一个实例(它没有文件描述符)。对于这种情况,我们只需要为实用程序添加一个守卫(就像when dev/null isn't writeable 的守卫一样)。这是一个很小的变化,但我现在很忙。 PR 将不胜感激!

因此,换句话说,这不是 FileWrapper 的问题(如 cmets 中所建议的),而是 Azure 的假 stderr 和 PILKit 的 quiet 实用程序的组合。

【讨论】:

  • 这似乎是一种奇怪的实现方式。据我了解,重新分配sys.stderr 是临时重定向stderr 的正常方法,它应该可以与StringIO 等配合使用。
  • 重新分配 Python 的 sys.stderr 不会做同样的事情。 PIL 的噪音实际上是在较低的水平上发生的。查看this SQ question 以更好地描述我们正在解决的问题。
  • 嗯...在这种情况下,“真正的”标准错误应该仍然指向 something。在大多数系统上,stderr 是 fd #2。如果它足够可靠,您也许可以对其进行硬编码。我还相信有一个 C 宏可以扩展到 stderr fd,以获得更大的可移植性。我想它可能会被关闭,但在这种情况下,“保留”它应该是微不足道的。
  • 我想我觉得尝试获取 fd 并在出现错误时退出比对描述符进行假设更舒服。但如果您对此有强烈的感觉,我们可以在 GH 上进行更多讨论。
猜你喜欢
  • 1970-01-01
  • 2018-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-22
  • 2016-02-02
  • 2015-06-22
相关资源
最近更新 更多