我知道我已经安装了所有包(pip freeze) 我在 Windows 10 机器上运行 python3.4,但遇到奇怪的依赖问题
测试.py:
from images2gif import writeGif
from PIL import Image, ImageSequence
import os
file_names = ['output\donkey-1.png', 'output\donkey-2.png']
images = [Image.open(fn) for fn in file_names]
size = (600,350)
for im in images:
im.thumbnail(size, Image.ANTIALIAS)
filename = "test.gif"
writeGif(filename, images, duration=0.5, subRectangles=False)
运行 test.py 出现以下错误,无法在网络上的其他任何地方找到此错误
Traceback (most recent call last):
File "test.py", line 2, in <module>
from images2gif import writeGif
File "C:\Python34\lib\site-packages\images2gif\__init__.py", line 1, in <module>
from images2gif import readGif as readGif
ImportError: cannot import name 'readGif'
最佳答案
我刚刚查看了 actual repository source code : 它使用 Python 2 风格的相对导入。也就是说,images2gif 不支持开箱即用的 Python 3。
目前的一个解决方案可能是下载 the sourcefile from pypi ,提取它(不确定 Windows 是否喜欢 tar.gz 文件),cd 进入目录并在其上运行 2to3:
2to3 -w .
然后手动安装:
python3.4 setup.py install
但可能更简单,只需查看 2to3
的结果:手动更改已安装包中的两个导入语句(在 __init__.py
中):
-from images2gif import readGif as readGif
-from images2gif import writeGif as writeGif
+from .images2gif import readGif as readGif
+from .images2gif import writeGif as writeGif
因为看起来其他一切都与 PY3K 兼容;这两个导入语句中 images2gif
前面的 pip 使其成为正确的 PY3K 相对导入。
关于image2gif 的 Python 导入问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32674597/