Skip to content

Allow package relative imports for directory style apps #9545

@bryevdv

Description

@bryevdv

Now that we are Python 3 only, it's fairly straightforward to enable things like

from .data import process_data

in the main.py of a directory style Bokeh server app. And even to have subpackages with __init__.py files work. This has been a fairly unpleasant pain point for some users.

A minimal diff just sets __path__ and __package__ but we will want to put it behind a flag so that it only happens for directory style apps:

diff --git a/bokeh/application/handlers/code_runner.py b/bokeh/application/handlers/code_runner.py
index 9b8c26f90..e1a75ae99 100644
--- a/bokeh/application/handlers/code_runner.py
+++ b/bokeh/application/handlers/code_runner.py
@@ -137,6 +137,9 @@ class CodeRunner(object):
         module_name = 'bk_script_' + make_id().replace('-', '')
         module = ModuleType(module_name)
         module.__dict__['__file__'] = os.path.abspath(self._path)
+        module.__package__ = module_name
+        from os.path import dirname
+        module.__path__ = [dirname(self._path)]

         return module

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions