Description
I experience two major problems when using relative imports.
I will demonstrate the issues using this example app:
- /config/pyscript/apps/myapp/__init__.py
from .subfolder.a import foo
from .subfolder.b import bar
- /config/pyscript/apps/myapp/subfolder/a.py
def foo():
pass
- /config/pyscript/apps/myapp/subfolder/b.py
from .subfolder.a import foo
def bar():
pass
The issues I encountered are:
- Relative imports are not really relative.
Relative imports are only relative to the root directory of the app. So to be able to import foo() from a.py in b.py one has to state from .subfolder.a import foo
. According to the Python specification the actual correct statement here should be from .a import foo
but it will not work here.
- Incorrect unloading of global contexts associated with relative imports when re-evaluating them after changes.
In the example above four global contexts are created: apps.myapp, apps.myapp.subfolder.a, apps.myapp.subfolder.b and apps.myapp.subfolder.b.subfolder.a. The modification or deletion of any unrelated script triggers a re-evaluating of all global contexts. During this re-evaluation pyscript cannot find a corresponding source file for the global context apps.myapp.subfolder.b.subfolder.a. Because of this it gets unloaded which breaks the app.