From c3073eaed5cc6b68539630f84da08e98ce8b9573 Mon Sep 17 00:00:00 2001
From: John Veitch <john.veitch@ligo.org>
Date: Mon, 16 Jan 2023 10:48:26 +0000
Subject: [PATCH] Add logic in case conda call doesn't work

---
 bilby/core/utils/log.py | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/bilby/core/utils/log.py b/bilby/core/utils/log.py
index e8042942a..4884eba9c 100644
--- a/bilby/core/utils/log.py
+++ b/bilby/core/utils/log.py
@@ -99,16 +99,21 @@ def env_package_list(as_dataframe=False):
 
     # if a conda-meta directory exists, this is a conda environment, so
     # use conda to print the package list
-    if (Path(prefix) / "conda-meta").is_dir():
-        pkgs = json.loads(subprocess.check_output([
-            "conda",
-            "list",
-            "--prefix", prefix,
-            "--json"
-        ]))
+    conda_detected = (Path(prefix) / "conda-meta").is_dir()
+    if conda_detected:
+        try:
+            pkgs = json.loads(subprocess.check_output([
+                "conda",
+                "list",
+                "--prefix", prefix,
+                "--json"
+            ]))
+        except (FileNotFoundError, subprocess.CalledProcessError):
+            # When a conda env is in use but conda is unavailable
+            conda_detected = False
 
     # otherwise try and use Pip
-    else:
+    if not conda_detected:
         try:
             import pip  # noqa: F401
         except ModuleNotFoundError:  # no pip?
-- 
GitLab