. , , imp importlib. :
import imp
import importlib
package_path = r"C:\path_to_package"
package_name = "module"
module_absolute_name = "module.sub_module"
module_relative_name = ".sub_module"
package_info = imp.find_module(package_name, [package_path])
package_module = imp.load_module(package_name, *package_info)
importlib.import_module(module_absolute_name, package_name)
importlib.import_module(module_relative_name, package_name)
This will allow us to import the sub_module using relative module paths, because we already loaded the parent package, and the submodule was loaded correctly by importlib to find out that it is imported relatively.
I believe that this solution is only necessary for those of us stuck in Python 2. *, but someone would need to confirm this.
AndyN source
share