From d2eee1c2e1be14c2e9f510524cb067211f8b5a52 Mon Sep 17 00:00:00 2001 From: bergi Date: Tue, 1 Mar 2022 16:24:25 +0100 Subject: [PATCH] use sys.prefix before trying sys.executable to find venv python executable (#3) --- web/park_api/management/commands/pa_scrape.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/web/park_api/management/commands/pa_scrape.py b/web/park_api/management/commands/pa_scrape.py index d06d162..814599f 100644 --- a/web/park_api/management/commands/pa_scrape.py +++ b/web/park_api/management/commands/pa_scrape.py @@ -155,10 +155,10 @@ def run_scraper_process( verbose: bool = False, ) -> Union[dict, list]: - if command == "scrape" and verbose: - print(f"module '{path.name}' scraping {pool_filter or 'all pools'}") + if verbose and command == "scrape": + print(f"module '{path.name}' scraping {pool_filter or 'all pools'}", file=sys.stderr) - args = [Path(sys.executable).resolve(), "scraper.py", command] + args = [python_executable(), "scraper.py", command] if pool_filter: args += ["--pools", *pool_filter] if caching is True: @@ -168,7 +168,7 @@ def run_scraper_process( try: if verbose: - print("running", " ".join(str(a) for a in args), "in directory", path) + print("running", " ".join(str(a) for a in args), "in directory", path, file=sys.stderr) process = subprocess.Popen( args=args, cwd=str(path), @@ -185,3 +185,12 @@ def run_scraper_process( except Exception as e: return {"error": f"{type(e).__name__}: {e}\n{traceback.format_exc()}"} + +def python_executable() -> str: + fn = Path(sys.prefix) / "bin" / "python" + if not fn.exists(): + fn = Path(sys.prefix) / "Scripts" / "python.exe" + if not fn.exists(): + fn = Path(sys.executable) + + return str(fn.resolve())