0

Hi i have written basic python unittest code below :

import unittest

class Phonebooktest(unittest.TestCase):

   def test_create_phonebook():
       print("welcome to python")



if __name__ == '__main__':
    unittest.main()`

i am getting an error :

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'

Someone please help me to resolve this error.

Łukasz Rogalski
  • 22,092
  • 8
  • 59
  • 93
jaibalaji
  • 3,159
  • 2
  • 15
  • 28
  • Is any of files in direcotry where `test_phone_book.py` resides is named `operator.py`? – Łukasz Rogalski Aug 03 '17 at 05:33
  • yes there is operator.py file. i just remove that & tried it worked. may i know why this happened? – jaibalaji Aug 03 '17 at 05:36
  • Possible duplicate of [Importing installed package from script raises "AttributeError: module has no attribute" or "ImportError: cannot import name"](https://stackoverflow.com/questions/36250353/importing-installed-package-from-script-raises-attributeerror-module-has-no-at) – Łukasz Rogalski Aug 03 '17 at 06:03

1 Answers1

1

In all likelihood you have a file that is named operator.py in your directory. Unfortunately, that name conflicts with Python's standard library, which has an operator module.

In fact, look at the last two lines, one of which says:

from operator import itemgetter as _itemgetter, eq as _eq
    ImportError: cannot import name 'itemgetter'

Which suggests it tries to get itemgetter from operator, but since you have an operator.py file in your working directory, it tries to import itemgetter from that file, not Python's standard library.

Try to rename your operator.py to something else and see what happens.

cegas
  • 2,823
  • 3
  • 16
  • 16