2

I want to Mock a method in a Python Test. In order to do this I need MagicMock which I try to import it like this:

from unittest import TestCase
from unittest.mock import MagicMock

Even after I clicked several times on install in PyCharm and although I see magicmock 0.0.3 under Preferences/Project Interpreter, I get this error:

 from unittest.mock import MagicMock
 ImportError: No module named mock

The import statement I saw here. What do I wrong?

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
John Smith
  • 6,105
  • 16
  • 58
  • 109

1 Answers1

5

In Py3.5 (basically versions >= 3.3) mock has been incorporated in the Standard Library as you linked in the documentation. So, the statement:

from unittest.mock import MagicMock

should never raise ImportErrors.


I'm guessing you're either using a Py3 version < 3.3 or are using Py2.7.

In these cases you should pip install mock first and then import:

from mock import MagicMock
Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253