无法在 Python 3.5.2 中导入 itertools
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38634810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Failing to import itertools in Python 3.5.2
提问by Joyita Das
I am new to Python. I am trying to import izip_longest from itertools. But I am not able to find the import "itertools" in the preferences in Python interpreter. I am using Python 3.5.2. It gives me the below error-
我是 Python 的新手。我正在尝试从 itertools 导入 izip_longest。但是我无法在 Python 解释器的首选项中找到导入“itertools”。我正在使用 Python 3.5.2。它给了我以下错误-
from itertools import izip_longest
ImportError: cannot import name 'izip_longest'
Please let me know what is the right course of action. I have tried Python 2.7 too and ended up with same problem. Do I need to use lower version Python.
请让我知道什么是正确的行动方案。我也尝试过 Python 2.7,但最终还是遇到了同样的问题。我是否需要使用较低版本的 Python。
回答by Martijn Pieters
izip_longest
was renamedto zip_longest
in Python 3 (note, no i
at the start), import that instead:
izip_longest
在 Python 3中重命名为zip_longest
(注意,i
一开始没有),改为导入:
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:
如果您需要编写同时适用于 Python 2 和 3 的代码,请ImportError
尝试使用其他名称,然后重命名:
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
回答by DILEEP_B
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
导入任何功能的一种简单方法是通过对象导入(例如:import itertools as it),除非您想隐藏其他功能。由于模块中的特性会根据 python 版本而变化,检查模块中是否存在该特性的简单方法是通过dir()函数。 import itertools as it dir(it) 它将列出其中的所有功能