Modules used in Blender scripts and add-ons are located in the same directory as the script or add-on, or in the Blender installation directory by the “blender_version\python\lib\” path. However, it is possible to load a module located elsewhere on the hard drive.
To do this, we need to load the module by its absolute path.
Let’s define with variables the full path to the module directory and the name under which the module will be loaded to the API:
1 2 |
path = 'd:\\dev\\python\\blender\\bpy_plus\\__init__.py' name = 'bpy_plus' |
Next, load the module using the “importlib”:
1 2 3 4 5 6 7 |
import importlib import sys spec = importlib.util.spec_from_file_location(name, path) module = importlib.util.module_from_spec(spec) sys.modules[spec.name] = module spec.loader.exec_module(module) |
Now we can use the loaded module in the usual way by its name.
1 2 3 4 5 |
from bpy_plus import about print(about.version()) # (1, 0, 0) |