对于Python 3.5+,请使用:
import importlib.utilspec = importlib.util.spec_from_file_location("module.name", "/path/to/file.py")foo = importlib.util.module_from_spec(spec)spec.loader.exec_module(foo)foo.MyClass()对于Python 3.3和3.4,请使用:
from importlib.machinery import SourceFileLoaderfoo = SourceFileLoader("module.name", "/path/to/file.py").load_module()foo.MyClass()(尽管在Python 3.4中已弃用此功能。)
对于Python 2,请使用:
import impfoo = imp.load_source('module.name', '/path/to/file.py')foo.MyClass()编译后的Python文件和DLL具有等效的便捷功能。



