I have the following piece of code that looks like it might be able to import a package that has the same name as the script that is being run, but it goes into infinite recursion:
def import_non_local2(name, custom_name=None):
custom_name = custom_name or name
from importlib.util import find_spec, module_from_spec
spec = find_spec(name, sys.path[1:])
mod = module_from_spec(spec)
spec.name = custom_name
spec.loader.exec_module(mod)
whois_service = import_non_local2('whois', 'whois_service')
I'm trying to convert the code from this answer to stop using the deprecated imp
.