ImportError: No module named mock
Asked Answered
G

7

106

So I am trying to use unittest.mock to mock some of my methods in my unit tests. I do:

from unittest.mock import MagicMock
f = open("data/static/mock_ffprobe_response")
subprocess.check_output = MagicMock(return_value=f.read())
f.close()

But I am getting:

ImportError: No module named mock

I tried:

pip install mock

It's still not working.

Greenman answered 16/7, 2012 at 9:27 Comment(3)
this module is available in version 3.3 ,, what is your versionPound
My version is 2.7. Is there any alternative?Greenman
According to Google this should work also on 2.7.Greenman
E
130

unittest is a built-in module; mock is an external library (pre-3.3 betas, anyway). After installing mock via pip install, you import it not by using

from unittest.mock import MagicMock

but

from mock import MagicMock

Edit: mock has been included in the unittest module (since Python3.3), and can be imported by import unittest.mock.

Etymology answered 16/7, 2012 at 9:33 Comment(1)
On Python 3.3+ you need to do the opposite of what this answer suggests: from unittest.mock import MagicMockBomke
P
24

For Python 2.7:

Install mock:

pip install mock

Then in the test code, use this import:

from mock import patch, MagicMock
Punkah answered 20/9, 2017 at 20:10 Comment(0)
P
17

If you want to support both, Python 2 and Python 3, you can also use following:

import sys
if sys.version_info >= (3, 3):
    from unittest.mock import MagicMock
else:
    from mock import MagicMock

or, if you don't want to import sys

try:
    from unittest.mock import MagicMock
except ImportError:
    from mock import MagicMock
Polydeuces answered 4/6, 2018 at 15:58 Comment(0)
B
17

If you're using Python 3.3+, change

import mock

to

from unittest import mock

Or you can keep all the code the same and run this shell command instead:

pip install mock

This command works on any Python version but changing the code is preferable because then other people running your code won't run into that import error anymore.

This error happens because unittest.mock was added in Python 3.3, and there is a backport on PyPI for older Python versions. So if your code used to be Python 2, it's probably trying to import the backport.


pyupgrade is a tool you can run on your code to rewrite those imports and remove other no-longer-useful leftovers from Python 2.

Bomke answered 7/11, 2019 at 21:56 Comment(0)
A
8

For some reason, import unittest.mock did not work for me, I had to do

from unittest import mock
Andizhan answered 21/4, 2018 at 20:42 Comment(0)
P
5

I was facing issues on import when running pytest. It turned out to be my pytest was of 2.7 version of python, while my virtualenv was a 3.6 python. I fixed it by doing a pip install from my virtualenv and then using the pytest from env/bin/pytest.

Passant answered 15/3, 2019 at 15:23 Comment(0)
P
3

For your version, I would suggest go to http://hg.python.org/cpython/file/default/Lib/unittest/mock.py and use this source to your advantage.

Pound answered 16/7, 2012 at 9:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.