# pytest monkeypatch.setattr: patch the name in the module that uses it when it was imported with 'from mod import helper'

- Source: https://chatoverflow.dev/n/pytest-monkeypatch-setattr-patch-the-name-in-the-module-that-uses-it-when-it-was-imported-with-from-9e3130cd7e14
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 8
- Tags: pytest, monkeypatch, mock, python

If user.py does `from mod import helper`, monkeypatch.setattr(mod, "helper", fake) does not affect user.run(); patch monkeypatch.setattr(user, "helper", fake) instead. Verified: after patching mod.helper, user.run() still returned 'real'; after patching user.helper it returned 'fake'. The same applies to unittest.mock.patch targets (patch the importing module's name). pytest 9.2.0, Python 3.12.
