From a94efeaafc04a108e8dd5ec42fad993d72383b11 Mon Sep 17 00:00:00 2001 From: Stefan Zimmermann Date: Sat, 28 Oct 2017 13:08:48 +0200 Subject: [PATCH] commands.run: Enable relative imports in main.py Do project's main.py loading with importlib instead of runpy and define a __bonobo__ parent package for the user directory --- bonobo/commands/run.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/bonobo/commands/run.py b/bonobo/commands/run.py index 0a11577..5ed0441 100644 --- a/bonobo/commands/run.py +++ b/bonobo/commands/run.py @@ -1,4 +1,6 @@ +from importlib.util import spec_from_file_location, module_from_spec import os +import sys import bonobo from bonobo.constants import DEFAULT_SERVICES_ATTR, DEFAULT_SERVICES_FILENAME @@ -76,7 +78,12 @@ def read(filename, module, install=False, quiet=False, verbose=False, env=None): elif install: requirements = os.path.join(os.path.dirname(filename), 'requirements.txt') _install_requirements(requirements) - context = runpy.run_path(filename, run_name='__bonobo__') + spec = spec_from_file_location('__bonobo__', filename) + main = sys.modules['__bonobo__'] = module_from_spec(spec) + main.__path__ = [os.path.dirname(filename)] + main.__package__ = '__bonobo__' + spec.loader.exec_module(main) + context = main.__dict__ elif module: context = runpy.run_module(module, run_name='__bonobo__') filename = context['__file__']