Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow package relative imports for directory style apps #9545

Closed
bryevdv opened this issue Dec 27, 2019 · 2 comments · Fixed by #9617
Closed

Allow package relative imports for directory style apps #9545

bryevdv opened this issue Dec 27, 2019 · 2 comments · Fixed by #9617

Comments

@bryevdv
Copy link
Member

bryevdv commented Dec 27, 2019

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
@bryevdv
Copy link
Member Author

bryevdv commented Dec 27, 2019

As a side note make_id currently by default just makes a sequential counter. We might want to update it to better guarantee uniqueness. Or possibly offer an option to specify the package name.

Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 27, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant