close

[Solved] ImportError: No module named mock

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error ImportError: No module named mock in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How ImportError: No module named mock Error Occurs?

Today I get the following error ImportError: No module named mock in python.

How To Solve ImportError: No module named mock Error ?

  1. How To Solve ImportError: No module named mock Error ?

    To Solve ImportError: No module named mock Error 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

  2. ImportError: No module named mock

    To Solve ImportError: No module named mock Error 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

Solution 1

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

Solution 2

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.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read