Python 3.6.1的单元测试出错

时间:2017-08-03 05:30:48

标签: python python-3.x unit-testing python-3.6

您好我已经在下面编写了基本的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'

有人请帮我解决此错误。

1 个答案:

答案 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重命名为其他内容,看看会发生什么。