How to Install mock in Python
Rolling backport of unittest.mock for all Pythons
pip install mock
What is mock?
Rolling backport of unittest.mock for all Pythons
mock is a library for testing in Python. It allows you to replace parts of your system under test with mock objects and make assertions about how they have been used.
mock is now part of the Python standard library, available as `unittest.mock ` in Python 3.3 onwards.
This package contains a rolling backport of the standard library mock code compatible with Python 3.6 and up.
Quick Start
Minimal example to get started with mock:
import mock
print(mock.__version__)
Installation
pip (standard)
pip install mock
Virtual environment (recommended)
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install mock
pip3
pip3 install mock
conda
conda install -c conda-forge mock
Poetry
poetry add mock
Verify the Installation
After installing, confirm the package is available:
python -c "import mock; print(mock.__version__)"
If this prints a version number, installation succeeded. If you see a ModuleNotFoundError, see the errors section below.
Installation Errors
Common errors when installing mock with pip.
ModuleNotFoundError: No module named 'mock'
Cause: The package is not installed in the current Python environment.
Fix: Run pip install mock. If using a virtual environment, ensure it is activated first.
ModuleNotFoundError: No module named 'mock' (installed but still failing)
Cause: pip installed the package into a different Python than the one running your script.
Fix: Use python -m pip install mock to install into the interpreter you are running.
ImportError: cannot import name 'X' from 'mock'
Cause: The function or class does not exist in the installed version.
Fix: Check the version with pip show mock and upgrade with pip install --upgrade mock.
pip: command not found
Cause: pip is not in PATH or Python was not added to PATH during installation.
Fix: Try python -m pip install mock. On macOS/Linux try pip3.
PermissionError: [Errno 13] Permission denied
Cause: No write access to the system Python package directory.
Fix: Use a virtual environment, or add --user: pip install --user mock
SSL: CERTIFICATE_VERIFY_FAILED
Cause: pip cannot verify PyPI's SSL certificate — common behind corporate proxies.
Fix: Try: pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org mock
Recent Releases
| Version | Released |
|---|---|
5.2.0 latest |
2025-03-03 |
5.1.0 |
2023-07-11 |
5.0.2 |
2023-04-16 |
5.0.1 |
2023-01-09 |
5.0.0 |
2022-12-28 |
Manage mock
Upgrade to latest version
pip install --upgrade mock
Install a specific version
pip install mock==5.2.0
Uninstall
pip uninstall mock
Check what is installed
pip show mock