You can use inspect.getmodule to get the module that owns a function:
import inspect
from math import sqrt
from importlib import reload
math = reload(inspect.getmodule(sqrt))
sqrt = math.sqrt
Using __qualname__ you can automate this process:
def reload_attr(attribute):
obj = reload(inspect.getmodule(attribute))
for name in attribute.__qualname__.split("."):
try:
obj = getattr(obj, name)
except NameError:
raise ValueError("Unable to find attribute.")
return obj
reload_attr(sqrt)
#>>> <built-in function sqrt>
Note that this isn't guaranteed to work; not all attributes are trivial to locate.
from module_name import one_functionthe whole module is loaded, just likeimport module_name. The only difference is what names get added to the global namespace.