I have a python module here /root/python/foo.py. I have a bunch of other modules here in the folder /root/lib/ which looks like this
lib/
|
├─ module1/
| ├─ __init__.py
| └─ bar.py
|
└─ module2/
├─ __init__.py
└─ bar.py
I would like to import /root/lib/module1 and /root/lib/module2 from foo.py. I would like to not have to add /root/lib/ to the python system path. This stack overflow answer tells you how to use either imp.load_source, importlib.machinery.SourceFileLoader, or the importlib.util class to load a module from a file (depending on the python version). I think these only work if the module is a single file. If I try something like this in Python 3.4
from importlib.machinery import SourceFileLoader
problem_module = SourceFileLoader('test_mod', '/root/lib/module1').load_module()
I get an IsADirectoryError
My question is whether there is a similar way to load a module (given its full path) if it is a directory, without adding the whole lib/ folder to the system path?
'after module1sys.pathto reliably import modules. See this question.libfolder to the system path isn't as bad as I thought.