I'm working on a macro engine that transforms YAML files. These YAML files contain paths to Python modules that I am importing using importlib
. I would like for end users to be able to specify relative paths beginning with .
, and for these paths to be resolved relative to the YAML file. (This way, a user could easily ship the YAML file and a related module in a directory or zip file.)
I would prefer not to modify sys.path
if possible, but this is not a hard requirement (I can use a context manager to patch/unpatch it).
I know how to use importlib.import_module(name, package)
to import name
relative to a dotted path package
. But here, I have an OS file path to the YAML file, which is not a Python module. Can this be done?
Example:
- My script is at
~/bin/macroengine.py
- The YAML file is at
~/example/source.yaml
- The external module is at
~/example/myModule.py
I would like for source.yaml
to reference the external module as .myModule
.