Fix: Windows DLL path lookup with Python >= 3.8
[babeltrace.git] / src / bindings / python / bt2 / bt2 / __init__.py
index 089c98b076191e52638bf63a2da9bad0c9617efd..2dd7dc36b69ae1cace2ad37fd62ab5d6ce5a07ab 100644 (file)
@@ -2,8 +2,24 @@
 #
 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
 
+import os
 import sys
 
+# With Python ≥ 3.8 on Windows, the DLL lookup mechanism to load native
+# modules doesn't search the `PATH` environment variable like everything
+# else on this platform.
+#
+# See <https://docs.python.org/3/whatsnew/3.8.html#bpo-36085-whatsnew>.
+#
+# Restore this behaviour by doing it manually.
+if os.name == "nt" and sys.version_info >= (3, 8):
+    for path in os.getenv("PATH", "").split(os.pathsep):
+        if os.path.exists(path) and path != ".":
+            os.add_dll_directory(path)
+
+del os
+
+
 from bt2.mip import get_maximal_mip_version, get_greatest_operative_mip_version
 from bt2.error import (
     ComponentClassType,
This page took 0.02228 seconds and 4 git commands to generate.