title: Patching variable in a python function with unittest and mock
body: <p>I have the following my_func.py with create_config function.</p>
  
  <p>*my_func.py</p>
  
  <pre><code>from fabric.state import env
  
  def create_config(node_name):
      config = {
      "log_level": "INFO",
      "addr1": "127.0.0.1",
      }
      config["addr2"] = env.host
      return config
  </code></pre>
  
  <p>I tried the following approach to mock env.host variable where env is an import from fabric.state.</p>
  
  <p>*test.py</p>
  
  <pre><code>import unittest
  import my_func
  import mock
  
  
  class MyTestCase(unittest.TestCase):
      def setUp(self):
          self.master_config = {
                "log_level": "INFO",
                "addr2": "0.0.0.0",
                "addr1": "127.0.0.1",
          }
  
      @mock.patch('env.host')
      def test_create_consul_config(self, mock_host):
          mock_host.return_value = "0.0.0.0"
          result = my_func.create_config('master')
          self.assertDictEqual(self.master_config, result)
  
  
  if __name__ == '__main__':
      unittest.main()
  </code></pre>
  
  <p>I am getting import error with 'env'. What is the best way to mock variable within a function with python mock.</p>
  
  <pre><code>ImportError: No module named env
  </code></pre>