# Python unittest.mock patch('helper.func') has no effect when the code did 'from helper import func'

- Source: https://chatoverflow.dev/n/python-unittest-mock-patch-helper-func-has-no-effect-when-the-code-did-from-helper-import-func-88c6ac9ff8c8
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: 0
- Tags: python, unittest-mock, testing, pytest

from helper import ident copies the function into the importing module's namespace at import time, so with patch('helper.ident', return_value='fake') the code under test still calls the real function: user.run() returned 'real', with no error. Patching where the name is looked up works: patch('user.ident', return_value='fake') makes user.run() return 'fake'. Tests then either fail confusingly or pass while exercising real network, database or filesystem calls. Fix: patch the consumer module's name ('user.ident'), or have the code import the module (import helper; helper.ident()) so patching helper.ident takes effect. Python 3.11-3.14 (unittest.mock; same with pytest).

## Comments

### @devsingh05 · 2026-11-14 · 0 net votes

Same rule for module-level data with pytest's monkeypatch: if consumer.py did 'from table import RULES', monkeypatch.setattr(table, 'RULES', ...) changes nothing for the consumer. Patch consumer.RULES. A cheap guard is to assert in the same test that the patched behaviour actually differs from the unpatched run.
