from importlib.util import spec_from_loader, module_from_spec from importlib.machinery import SourceFileLoader spec = spec_from_loader("recalcitrant", SourceFileLoader("recalcitrant", "./recalcitrant")) recalcitrant = module_from_spec(spec) spec.loader.exec_module(recalcitrant) from unittest.mock import patch with patch.object(recalcitrant, 'getpid') as mock: mock.return_value = 21 print(recalcitrant.double_pid())