您好我已经在下面编写了基本的python单元测试代码:
import unittest
class Phonebooktest(unittest.TestCase):
def test_create_phonebook():
print("welcome to python")
if __name__ == '__main__':
unittest.main()`
我收到错误:
25
/ 2
10>>2 2
Traceback (most recent call last):
File "test_phone_book.py", line 1, in <module>
import unittest
File "C:\Users\XXXXX\AppData\Local\Programs\Python\Python36\lib\unittest\__init__.py", line 58, in <module>
from .result import TestResult
File "C:\Users\XXXXX\AppData\Local\Programs\Python\Python36\lib\unittest\result.py", line 5, in <module>
import traceback
File "C:\Users\XXXXX\AppData\Local\Programs\Python\Python36\lib\traceback.py", line 3, in <module>
import collections
File "C:\Users\XXXXX\AppData\Local\Programs\Python\Python36\lib\collections\__init__.py", line 26, in <module>
from operator import itemgetter as _itemgetter, eq as _eq
ImportError: cannot import name 'itemgetter'
有人请帮我解决此错误。
答案 0 :(得分:1)
您很可能在目录中有一个名为operator.py
的文件。不幸的是,该名称与Python的标准库冲突,后者具有operator
模块。
事实上,看看最后两行,其中一行是:
from operator import itemgetter as _itemgetter, eq as _eq
ImportError: cannot import name 'itemgetter'
这表明它尝试从itemgetter
获取operator
,但由于您的工作目录中有operator.py
个文件,它会尝试从该文件导入itemgetter
,不是Python的标准库。
尝试将operator.py
重命名为其他内容,看看会发生什么。