今天在使用一个tensorflow项目时遇到这个DLL导入问题,总算是遇到了第一次需要看源码解决的问题.
ImportError Traceback (most recent call last)
<ipython-input-3-0af60833c10b> in <module>()
6 import numpy as np
7 #from pillow import Image
----> 8 from PIL import Image
9 from sklearn.model_selection import train_test_split
10 from sklearn.preprocessing import LabelBinarizer
D:\Users\52489\Anaconda3\lib\site-packages\PIL\Image.py in <module>()
56 # Also note that Image.core is not a publicly documented interface,
57 # and should be considered private and subject to change.
---> 58 from . import _imaging as core
59 if PILLOW_VERSION != getattr(core, 'PILLOW_VERSION', None):
60 raise ImportError("The _imaging extension was built for another "
ImportError: DLL load failed: 找不到指定的模块。
解决方法:
conda uninstall pillow
conda update pip
pip install pillow
再次运行from PIL import Image就成功了
问题讨论:
跟着提示的错误到from . import _imaging as core里面看看
发现了几个pyd文件
问题基本可以定位为python自身对于c++产生的pyd文件(即DLL文件)的编译出现了问题,问题基本涉及电脑版本、python版本和pillow版本。这问题到了stackoverflow上也没有太适合的解决方法,去年的一个答案里面是通过把4.1.0的版本的pillow退回到4.0.0上。但因为我习惯尽早升级python和module的版本,所以去年的方案也已经不work,提示我已经有pillow的其他依赖库与4.0.0的pillow不匹配。
而pillow官方又已经在4.1.1中解决了这个问题,因此问题就是conda-forge获取的pillow包存在某种缺失(如不支持64位python),使用pip获取的pillow就正常了。