Skip to main content
 首页 » 编程设计

python之ImportError:无法从 Django Admin Pillow 导入名称 _imaging 访问

2025年04月02日41bonelee

我已经为这个错误苦苦挣扎了两天,尝试了堆栈溢出的所有答案,但没有成功。我有一个使用 Django 图像字段的简单模型

class Author(models.Model): 
    first_name = models.CharField(max_length=30) 
    last_name = models.CharField(max_length=30) 
    email = models.EmailField() 
    url = models.URLField(blank=True, null=True) 
    short_bio = models.TextField(max_length=200, blank=True, null=True) 
    long_bio = models.TextField(max_length=5000, blank=True, null=True) 
    role = models.ManyToManyField(AuthorRole) 
    facebook_link = models.URLField(blank=True, null=True) 
    linkedin_link = models.URLField(blank=True, null=True) 
    twitter_link = models.URLField(blank=True, null=True) 
    gplus_link = models.URLField(blank=True, null=True) 
    thumbnail = models.ImageField(upload_to='images/',     default='images/user_default.jpg') 

在生产服务器中,当我从管理员访问模型并选择图像并尝试保存时,它会抛出以下错误。我多次安装和卸载枕头。尝试了不同版本的 django 和 pillow。顺便说一下,它在本地环境中运行良好。

[:error] [pid 20256:tid 139822013380352] [remote 72.48.102.12:60881] 
from PIL import Image 
[:error] [pid 20256:tid 139822013380352] [remote 72.48.102.12:60881]       File"/home/.virtualenvs/wcsenvpython3/lib/python3.4/sitepackages/PIL/Image.py",    line 67, in <module> 
[:error] [pid 20256:tid 139822013380352] [remote 72.48.102.12:60881] 
from PIL import _imaging as core 
[:error] [pid 20256:tid 139822013380352] [remote 72.48.102.12:60881] ImportError:cannot import name _imaging 

我可以从 manage.py shell 中执行 from PIL import _imaging。所以看起来 pythonpath 配置正确。

在我的 virtualenv 中我可以看到 _imaging.cpython-34m.so 文件,但是没有 _imaging.py 文件。

我的服务器托管在 linode 中。这是 Ubuntu 14.04。我正在使用 Apache2。 Python 3.4.3。 Django 1.10 枕头 3.3.0。非常感谢您的帮助。这个错误困扰了我很长时间。

1 : http://i.stack.imgur.com/nH8O3.jpg 2 : http://i.stack.imgur.com/Vpdoe.jpg

请您参考如下方法:

好吧,我终于得到了答案。它与枕头无关。我将公共(public)写入权限授予保存图像的“媒体”文件夹,突然它解决了问题。我不确定这是否是一个安全漏洞,但它解决了错误。

我是怎么找到它的:
我决定从 PIL 更改 Image.py 文件。

  try: 
    # If the _imaging C module is not present, Pillow will not load. 
    # Note that other modules should not refer to _imaging directly; 
    # import Image and use the Image.core variable instead. 
    # Also note that Image.core is not a publicly documented interface, 
    # and should be considered private and subject to change. 
    from PIL import _imaging as core 
    if PILLOW_VERSION != getattr(core, 'PILLOW_VERSION', None): 
        raise ImportError("The _imaging extension was built for another " 
                          " version of Pillow or PIL") 
 
except ImportError as v: 
    core = _imaging_not_installed() 
    # Explanations for ways that we know we might have an import error 
    if str(v).startswith("Module use of python"): 
        # The _imaging C module is present, but not compiled for 
        # the right version (windows only).  Print a warning, if 
        # possible. 
        warnings.warn( 
            "The _imaging extension was built for another version " 
            "of Python.", 
            RuntimeWarning 
            ) 
    elif str(v).startswith("The _imaging extension"): 
        warnings.warn(str(v), RuntimeWarning) 
    elif "Symbol not found: _PyUnicodeUCS2_" in str(v): 
        # should match _PyUnicodeUCS2_FromString and 
        # _PyUnicodeUCS2_AsLatin1String 
        warnings.warn( 
            "The _imaging extension was built for Python with UCS2 support; " 
            "recompile Pillow or build Python --without-wide-unicode. ", 
            RuntimeWarning 
            ) 
    elif "Symbol not found: _PyUnicodeUCS4_" in str(v): 
        # should match _PyUnicodeUCS4_FromString and 
        # _PyUnicodeUCS4_AsLatin1String 
        warnings.warn( 
            "The _imaging extension was built for Python with UCS4 support; " 
            "recompile Pillow or build Python --with-wide-unicode. ", 
            RuntimeWarning 
            ) 
    # Fail here anyway. Don't let people run with a mostly broken Pillow. 
    # see docs/porting.rst 
    raise 

except block 正在检查几个条件,最后一行再次引发导入错误。我注释掉了 raise ,宾果游戏显示了权限错误。 我不知道为什么在问题是权限时它会显示导入错误。我希望 pillow 的作者能够研究这个问题并尝试生成相关的错误消息,而问题并不是真正的导入错误而是权限错误。