def find_path(name):
# Reject invalid module names
if not is_valid_name(name):
raise ValueError(name + " is not a valid module name")
name = str(name).strip()
# Check if built-in
if name in sys.builtin_module_names:
return "<built-in>"
# Check all sys.path locations
for p in sys.path:
loc = os.path.abspath(os.path.join(p, name + ".py"))
if os.path.exists(loc) and os.path.isfile(loc):
return loc
# Last resort, import module and check __file__
try:
mod = __import__(name)
except ImportError:
# Everything failed, assume nonexistant
return "<string>"
return mod.__file__ if hasattr(mod, "__file__") else "<built-in>"
评论列表
文章目录