From 0b6bf2b05b14a14fc5b321efca24c6a730cc7a30 Mon Sep 17 00:00:00 2001 From: thealanjason <40885942+thealanjason@users.noreply.github.com> Date: Sun, 11 Feb 2024 07:30:08 +0530 Subject: [PATCH 1/7] Add DLL Search Paths for Windows with Python 3.8+ Aims to Fix #457 --- pysrc/juliacall/__init__.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pysrc/juliacall/__init__.py b/pysrc/juliacall/__init__.py index ad8627b0..c12d19fe 100644 --- a/pysrc/juliacall/__init__.py +++ b/pysrc/juliacall/__init__.py @@ -50,6 +50,22 @@ def backtrace(self): def init(): import os + ## For Windows and Python 3.8+ need to add dll search paths + import sys + if os.name == "nt" and sys.hexversion >= 0x308000: + conda_environment_path = os.environ["CONDA_PREFIX"] + dll_search_paths = [ + conda_environment_path, + os.path.join(conda_environment_path, "Library", "mingw-w64", "bin"), + os.path.join(conda_environment_path, "Library", "usr", "bin"), + os.path.join(conda_environment_path, "Library", "bin"), + os.path.join(conda_environment_path, "Scripts"), + os.path.join(conda_environment_path, "bin") + ] + for path in dll_search_paths: + if os.path.exists(path): + os.add_dll_directory(path) + import ctypes as c import sys import subprocess From f29ded5506182f4bf553096003ad512a1c7fa74c Mon Sep 17 00:00:00 2001 From: thealanjason <40885942+thealanjason@users.noreply.github.com> Date: Sun, 11 Feb 2024 07:51:11 +0530 Subject: [PATCH 2/7] Try to make change compatible with JuliaCall and PythonCall --- pysrc/juliacall/__init__.py | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/pysrc/juliacall/__init__.py b/pysrc/juliacall/__init__.py index c12d19fe..90123167 100644 --- a/pysrc/juliacall/__init__.py +++ b/pysrc/juliacall/__init__.py @@ -50,27 +50,30 @@ def backtrace(self): def init(): import os - ## For Windows and Python 3.8+ need to add dll search paths - import sys - if os.name == "nt" and sys.hexversion >= 0x308000: - conda_environment_path = os.environ["CONDA_PREFIX"] - dll_search_paths = [ - conda_environment_path, - os.path.join(conda_environment_path, "Library", "mingw-w64", "bin"), - os.path.join(conda_environment_path, "Library", "usr", "bin"), - os.path.join(conda_environment_path, "Library", "bin"), - os.path.join(conda_environment_path, "Scripts"), - os.path.join(conda_environment_path, "bin") - ] - for path in dll_search_paths: - if os.path.exists(path): - os.add_dll_directory(path) - + add_dll_search_paths() import ctypes as c import sys import subprocess import warnings + def add_dll_search_paths(): + if "CONDA_PREFIX" in os.environ: #It does only when calling from PythonCall + ## For Windows and Python 3.8+ we need to add dll search paths + import sys + if os.name == "nt" and sys.hexversion >= 0x308000: + conda_environment_path = os.environ["CONDA_PREFIX"] + dll_search_paths = [ + conda_environment_path, + os.path.join(conda_environment_path, "Library", "mingw-w64", "bin"), + os.path.join(conda_environment_path, "Library", "usr", "bin"), + os.path.join(conda_environment_path, "Library", "bin"), + os.path.join(conda_environment_path, "Scripts"), + os.path.join(conda_environment_path, "bin") + ] + for path in dll_search_paths: + if os.path.exists(path): + os.add_dll_directory(path) + # importing pytorch before juliacall sometimes causes segfaults. TODO: remove if "torch" in sys.modules: warnings.warn( From 1c185a0aa19a03055129346e417abb21e93bef0e Mon Sep 17 00:00:00 2001 From: thealanjason <40885942+thealanjason@users.noreply.github.com> Date: Sun, 11 Feb 2024 07:54:49 +0530 Subject: [PATCH 3/7] Avoid making function --- pysrc/juliacall/__init__.py | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/pysrc/juliacall/__init__.py b/pysrc/juliacall/__init__.py index 90123167..8a6a0428 100644 --- a/pysrc/juliacall/__init__.py +++ b/pysrc/juliacall/__init__.py @@ -50,29 +50,28 @@ def backtrace(self): def init(): import os - add_dll_search_paths() + if "CONDA_PREFIX" in os.environ: #It does only when calling from PythonCall + ## For Windows and Python 3.8+ we need to add dll search paths + import sys + if os.name == "nt" and sys.hexversion >= 0x308000: + conda_environment_path = os.environ["CONDA_PREFIX"] + dll_search_paths = [ + conda_environment_path, + os.path.join(conda_environment_path, "Library", "mingw-w64", "bin"), + os.path.join(conda_environment_path, "Library", "usr", "bin"), + os.path.join(conda_environment_path, "Library", "bin"), + os.path.join(conda_environment_path, "Scripts"), + os.path.join(conda_environment_path, "bin") + ] + for path in dll_search_paths: + if os.path.exists(path): + os.add_dll_directory(path) import ctypes as c import sys import subprocess import warnings - def add_dll_search_paths(): - if "CONDA_PREFIX" in os.environ: #It does only when calling from PythonCall - ## For Windows and Python 3.8+ we need to add dll search paths - import sys - if os.name == "nt" and sys.hexversion >= 0x308000: - conda_environment_path = os.environ["CONDA_PREFIX"] - dll_search_paths = [ - conda_environment_path, - os.path.join(conda_environment_path, "Library", "mingw-w64", "bin"), - os.path.join(conda_environment_path, "Library", "usr", "bin"), - os.path.join(conda_environment_path, "Library", "bin"), - os.path.join(conda_environment_path, "Scripts"), - os.path.join(conda_environment_path, "bin") - ] - for path in dll_search_paths: - if os.path.exists(path): - os.add_dll_directory(path) + # importing pytorch before juliacall sometimes causes segfaults. TODO: remove if "torch" in sys.modules: From 0b1dea128c5601318f55da644dd67bce697740b2 Mon Sep 17 00:00:00 2001 From: thealanjason <40885942+thealanjason@users.noreply.github.com> Date: Mon, 12 Feb 2024 04:41:11 +0530 Subject: [PATCH 4/7] Update feedback from review --- pysrc/juliacall/__init__.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/pysrc/juliacall/__init__.py b/pysrc/juliacall/__init__.py index 8a6a0428..5cc50ceb 100644 --- a/pysrc/juliacall/__init__.py +++ b/pysrc/juliacall/__init__.py @@ -50,19 +50,15 @@ def backtrace(self): def init(): import os - if "CONDA_PREFIX" in os.environ: #It does only when calling from PythonCall + if os.environ.get("CONDA_PREFIX", "") != "": #Only when calling from PythonCall ## For Windows and Python 3.8+ we need to add dll search paths import sys - if os.name == "nt" and sys.hexversion >= 0x308000: + import platform + if platform.system() == "Windows" and sys.hexversion >= 0x308000: conda_environment_path = os.environ["CONDA_PREFIX"] dll_search_paths = [ - conda_environment_path, - os.path.join(conda_environment_path, "Library", "mingw-w64", "bin"), - os.path.join(conda_environment_path, "Library", "usr", "bin"), os.path.join(conda_environment_path, "Library", "bin"), - os.path.join(conda_environment_path, "Scripts"), - os.path.join(conda_environment_path, "bin") - ] + ] for path in dll_search_paths: if os.path.exists(path): os.add_dll_directory(path) @@ -72,7 +68,6 @@ def init(): import warnings - # importing pytorch before juliacall sometimes causes segfaults. TODO: remove if "torch" in sys.modules: warnings.warn( From 81c7d582f1649f869891ca595aa5c1365be53e60 Mon Sep 17 00:00:00 2001 From: thealanjason <40885942+thealanjason@users.noreply.github.com> Date: Mon, 12 Feb 2024 04:41:55 +0530 Subject: [PATCH 5/7] delete extra line --- pysrc/juliacall/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pysrc/juliacall/__init__.py b/pysrc/juliacall/__init__.py index 5cc50ceb..1c6358b3 100644 --- a/pysrc/juliacall/__init__.py +++ b/pysrc/juliacall/__init__.py @@ -67,7 +67,6 @@ def init(): import subprocess import warnings - # importing pytorch before juliacall sometimes causes segfaults. TODO: remove if "torch" in sys.modules: warnings.warn( From 60de4614f3387d86caaa1d432b2b5fc391ffdd47 Mon Sep 17 00:00:00 2001 From: thealanjason <40885942+thealanjason@users.noreply.github.com> Date: Mon, 12 Feb 2024 04:49:56 +0530 Subject: [PATCH 6/7] Delete for loop Co-authored-by: Miles Cranmer --- pysrc/juliacall/__init__.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/pysrc/juliacall/__init__.py b/pysrc/juliacall/__init__.py index 1c6358b3..722bfd69 100644 --- a/pysrc/juliacall/__init__.py +++ b/pysrc/juliacall/__init__.py @@ -55,13 +55,9 @@ def init(): import sys import platform if platform.system() == "Windows" and sys.hexversion >= 0x308000: - conda_environment_path = os.environ["CONDA_PREFIX"] - dll_search_paths = [ - os.path.join(conda_environment_path, "Library", "bin"), - ] - for path in dll_search_paths: - if os.path.exists(path): - os.add_dll_directory(path) + dll_search_path = os.path.join(os.environ["CONDA_PREFIX"], "Library", "bin") + if os.path.exists(dll_search_path): + os.add_dll_directory(dll_search_path) import ctypes as c import sys import subprocess From 1feedc317946894fa3476f41e7588117d7e46d37 Mon Sep 17 00:00:00 2001 From: thealanjason <40885942+thealanjason@users.noreply.github.com> Date: Mon, 12 Feb 2024 04:50:45 +0530 Subject: [PATCH 7/7] use version_info instead of hexversion Co-authored-by: Miles Cranmer --- pysrc/juliacall/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pysrc/juliacall/__init__.py b/pysrc/juliacall/__init__.py index 722bfd69..f6396a72 100644 --- a/pysrc/juliacall/__init__.py +++ b/pysrc/juliacall/__init__.py @@ -54,7 +54,7 @@ def init(): ## For Windows and Python 3.8+ we need to add dll search paths import sys import platform - if platform.system() == "Windows" and sys.hexversion >= 0x308000: + if platform.system() == "Windows" and sys.version_info >= (3, 8): dll_search_path = os.path.join(os.environ["CONDA_PREFIX"], "Library", "bin") if os.path.exists(dll_search_path): os.add_dll_directory(dll_search_path)