Skip to content

Relative import not working as intended #726

Open
@matzman666

Description

@matzman666

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:

  1. 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.

  1. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions