title: Python Unit-test Mocking, unable to patch `time` module that's being imported in `__init__.py`
body: <p>I've the following code in the <strong>__init__.py</strong> file</p>
  
  <pre><code>from time import sleep
  from functools import wraps
  
  def multi_try(func):
      @wraps(func)
      def inner(*args, **kwargs):
          count = 0
          while count &lt; 5:
              resp = func(*args, **kwargs)
              if resp.status_code in [200, 201]:
                  return resp
              sleep(1)
              count += 1
      return inner
  </code></pre>
  
  <p>While writing tests for the above decorator I'm not able to patch the the <em>time.sleep</em> properly.  </p>
  
  <p>See the test below, even though I've patched time module, still the sleep function inside the decorator getting called, thereby test case require 5+ seconds to finish.</p>
  
  <pre><code>def test_multi_try_time():
      with patch("time.sleep") as tm: 
         mocker = MagicMock(name="mocker")
         mocker.__name__ = "blah"
         resp_mock = MagicMock()
         resp_mock.status_code=400
         _json = '{"test":"twist"}'
         resp_mock.json=_json
         mocker.return_value = resp_mock
         wrapped = multi_try(mocker)
         resp = wrapped("p", "q")
         assert mocker.call_count == 5
         mocker.assert_called_with('p', 'q')
         assert resp == None
  </code></pre>
  
  <p>Also I tried this,</p>
  
  <p><code>with patch("dir.__init__.time" ) as tm:</code> </p>
  
  <p>and </p>
  
  <p><code>with patch("dir.utils.time" ) as tm:</code></p>
  
  <p>That resulted in </p>
  
  <p><code>AttributeError: &lt;module 'dir/__init__.pyc'&gt; does not have the attribute 'time'</code></p>