Failing to import itertools in Python 3.5.2

て烟熏妆下的殇ゞ 提交于 2019-12-03 12:07:51
Martijn Pieters

izip_longest was renamed to zip_longest in Python 3 (note, no i at the start), import that instead:

from itertools import zip_longest

and use that name in your code.

If you need to write code that works both on Python 2 and 3, catch the ImportError to try the other name, then rename:

try:
    # Python 3
    from itertools import zip_longest
except ImportError:
    # Python 2
    from itertools import izip_longest as zip_longest

# use the name zip_longest

One of the simple way of importing any feature is through object importing(ex: import itertools as it) unless you want to hide other features. Since features in modules does change according to python version, Easy way to check whether the feature is present in module is through dir() function. import itertools as it dir(it) It'll list all the features in it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!