From 42605286456c4a61bc2c4423a1415c3943e121bc Mon Sep 17 00:00:00 2001 From: Ashley Sommer Date: Mon, 4 Mar 2019 03:03:26 +1000 Subject: [PATCH] Fix the auto_reloader to work when the executable was launched with a module, rather than a script. (#1501) --- sanic/reloader_helpers.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/sanic/reloader_helpers.py b/sanic/reloader_helpers.py index 832353d8..b58391f6 100644 --- a/sanic/reloader_helpers.py +++ b/sanic/reloader_helpers.py @@ -36,7 +36,15 @@ def _iter_module_files(): def _get_args_for_reloading(): """Returns the executable.""" rv = [sys.executable] - rv.extend(sys.argv) + main_module = sys.modules["__main__"] + mod_spec = getattr(main_module, "__spec__", None) + if mod_spec: + # Parent exe was launched as a module rather than a script + rv.extend(["-m", mod_spec.name]) + if len(sys.argv) > 1: + rv.extend(sys.argv[1:]) + else: + rv.extend(sys.argv) return rv @@ -44,6 +52,7 @@ def restart_with_reloader(): """Create a new process and a subprocess in it with the same arguments as this one. """ + cwd = os.getcwd() args = _get_args_for_reloading() new_environ = os.environ.copy() new_environ["SANIC_SERVER_RUNNING"] = "true" @@ -51,7 +60,7 @@ def restart_with_reloader(): worker_process = Process( target=subprocess.call, args=(cmd,), - kwargs=dict(shell=True, env=new_environ), + kwargs={"cwd": cwd, "shell": True, "env": new_environ}, ) worker_process.start() return worker_process