Mocking a script with no `if __name__=="__main__":` block

Suppose I have files main.py and common.py in my root directory. These represent third party modules which I would not like to touch for the time being. Suppose I also have a test.py file which I'd like to use to test main.py.

common.py contains the following:

def hello():
  print("hello")

main.py contains the following:

from common import hello

hello()

Is there a way to monkey patch the hello function in test.py such that importing main.py will use the mocked function? For instance, I'd like to monkey patch hello to print out goodbye.


Pretty straightforward:

import common

common.hello = lambda: print("goodbye")

import main