python
module_error
ai_generated
true
ModuleNotFoundError: No module named 'mock'
ID: python/unittest-mock-not-found
80%Fix Rate
89%Confidence
0Evidence
2024-01-17First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.8 | active | — | — | — |
| 3.12 | active | — | — | — |
Root Cause
Code imports the standalone backport `mock` package, but it is not installed (or the project targets Python 3 where unittest.mock should be used). The backport was only needed for Python 2 or very old Python 3.
generic中文
代码导入了独立的 `mock` 后端包,但未安装(或项目面向 Python 3,应使用 unittest.mock)。该后端包仅在 Python 2 或很旧的 Python 3 中才需要。
Workarounds
-
97% success
Replace the import with the stdlib module: # from mock import patch (remove) from unittest.mock import patch, MagicMock On Python 3.3+ this requires no installation.
-
90% success
If legacy code must keep `import mock`, install the backport: `pip install mock` and add it to requirements-dev.txt.
Dead Ends
Common approaches that don't work:
-
85% fail
The test then runs without any mocking, so the real network or DB is hit and tests fail or hang for a different reason.
-
80% fail
Old mock versions are incompatible with modern patch targets and pytest, causing AttributeError or patching failures.