def patch_fake_gpio():
    print('Warning, not in RPi, using mock GPIO')
    # Idea taken from RPLCD who commented it as being from:
    # reddit.com/r/Python/comments/5eddp5/mock_testing_rpigpio
    import mock
    from .gpio import Gpio as FakeGpio
    MockRPi = mock.MagicMock()
    MockRPi.GPIO = FakeGpio()
    modules = {
        'RPi': MockRPi,
        'RPi.GPIO': MockRPi.GPIO,
    }
    patcher = mock.patch.dict('sys.modules', modules)
    patcher.start()


# Do the test if we have RPi.GPIO or not
ON_RPI = True
try:
    import RPi.GPIO
except ImportError:
    ON_RPI = False
if not ON_RPI:
    patch_fake_gpio()
# now that the patching is done, we can import RPLCD anywhere


def patch_fake_gpio():
    print('Warning, not in RPi, using mock GPIO')
    # Idea taken from RPLCD who commented it as being from:
    # reddit.com/r/Python/comments/5eddp5/mock_testing_rpigpio
    import mock
    from .gpio import Gpio as FakeGpio
    MockRPi = mock.MagicMock()
    MockRPi.GPIO = FakeGpio()
    modules = {
        'RPi': MockRPi,
        'RPi.GPIO': MockRPi.GPIO,
    }
    sys.modules.update(modules)
    is_active = True


# Do the test if we have RPi.GPIO or not
ON_RPI = True
try:
    import RPi.GPIO
except ImportError:
    ON_RPI = False
if not ON_RPI:
    patch_fake_gpio()
# now that the patching is done, we can import RPLCD anywhere


Make override more global, not just within patch scope
Make override more global, not just within patch scope

  def patch_fake_gpio():
      print('Warning, not in RPi, using mock GPIO')
      # Idea taken from RPLCD who commented it as being from:
      # reddit.com/r/Python/comments/5eddp5/mock_testing_rpigpio
      import mock
      from .gpio import Gpio as FakeGpio
      MockRPi = mock.MagicMock()
      MockRPi.GPIO = FakeGpio()
      modules = {
          'RPi': MockRPi,
          'RPi.GPIO': MockRPi.GPIO,
      }
-     patcher = mock.patch.dict('sys.modules', modules)
-     patcher.start()
+     sys.modules.update(modules)
+     is_active = True
  
  
  # Do the test if we have RPi.GPIO or not
  ON_RPI = True
  try:
      import RPi.GPIO
  except ImportError:
      ON_RPI = False
  if not ON_RPI:
      patch_fake_gpio()
  # now that the patching is done, we can import RPLCD anywhere
  
  
Make override more global, not just within patch scope
## Code Before:
def patch_fake_gpio():
    print('Warning, not in RPi, using mock GPIO')
    # Idea taken from RPLCD who commented it as being from:
    # reddit.com/r/Python/comments/5eddp5/mock_testing_rpigpio
    import mock
    from .gpio import Gpio as FakeGpio
    MockRPi = mock.MagicMock()
    MockRPi.GPIO = FakeGpio()
    modules = {
        'RPi': MockRPi,
        'RPi.GPIO': MockRPi.GPIO,
    }
    patcher = mock.patch.dict('sys.modules', modules)
    patcher.start()


# Do the test if we have RPi.GPIO or not
ON_RPI = True
try:
    import RPi.GPIO
except ImportError:
    ON_RPI = False
if not ON_RPI:
    patch_fake_gpio()
# now that the patching is done, we can import RPLCD anywhere


## Instruction:
Make override more global, not just within patch scope
## Code After:
def patch_fake_gpio():
    print('Warning, not in RPi, using mock GPIO')
    # Idea taken from RPLCD who commented it as being from:
    # reddit.com/r/Python/comments/5eddp5/mock_testing_rpigpio
    import mock
    from .gpio import Gpio as FakeGpio
    MockRPi = mock.MagicMock()
    MockRPi.GPIO = FakeGpio()
    modules = {
        'RPi': MockRPi,
        'RPi.GPIO': MockRPi.GPIO,
    }
    sys.modules.update(modules)
    is_active = True


# Do the test if we have RPi.GPIO or not
ON_RPI = True
try:
    import RPi.GPIO
except ImportError:
    ON_RPI = False
if not ON_RPI:
    patch_fake_gpio()
# now that the patching is done, we can import RPLCD anywhere

