There are a lot of python questions about how to import relative packages or by explicit location (linked to two popular examples).
In addition there is always the documentation
Having read this, I am still not quite sure what specs are, how they are related to modules, and why one would need to tokenize it.
So for someone who doesn't understand, could you please try to explain how one would do this (programmatically and what the means under the hood)
e.g.
if I have
proj-dir
--mod1
--|--__init__.py
--|--class1.py
--mod2
--|--__init__.py
--|--class2.py
how do I import mod2 into mod1?
import sys
sys.path.insert(0, "../mod2")
this technically works, but I fear that it may cause issues in the future if I try to pickle objects and use them elsewhere...
The explicit location suggested
import importlib.util
spec = 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()
so in this case I just do:
import importlib.util
spec = importlib.util.spec_from_file_location("mod2.class2", "../mod2/class2.py")
foo = importlib.util.module_from_spec(spec)
spec.loader.exec_module(foo)
foo.MyClass()
??