Spaces:
Sleeping
Sleeping
MilesCranmer
commited on
Commit
•
e87a1d6
1
Parent(s):
375cf2f
Use subprocess to find Julia env dir for <=1.6
Browse files- pysr/julia_helpers.py +29 -11
pysr/julia_helpers.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
"""Functions for initializing the Julia environment and installing deps."""
|
2 |
import sys
|
|
|
3 |
import warnings
|
4 |
from pathlib import Path
|
5 |
import os
|
@@ -28,16 +29,27 @@ def load_juliainfo():
|
|
28 |
return juliainfo
|
29 |
|
30 |
|
31 |
-
def
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
|
37 |
def _set_julia_project_env(julia_project, is_shared):
|
38 |
if is_shared:
|
39 |
if is_julia_version_greater_eq(version=(1, 7, 0)):
|
40 |
os.environ["JULIA_PROJECT"] = "@" + str(julia_project)
|
|
|
|
|
|
|
41 |
else:
|
42 |
os.environ["JULIA_PROJECT"] = str(julia_project)
|
43 |
|
@@ -85,9 +97,6 @@ def install(julia_project=None, quiet=False): # pragma: no cover
|
|
85 |
" so that the Julia environment is properly initialized."
|
86 |
)
|
87 |
|
88 |
-
# Ensure that the JuliaInfo is reset:
|
89 |
-
reset_juliainfo()
|
90 |
-
|
91 |
|
92 |
def import_error_string(julia_project=None):
|
93 |
s = """
|
@@ -144,17 +153,26 @@ def check_for_conflicting_libraries(): # pragma: no cover
|
|
144 |
def init_julia(julia_project=None):
|
145 |
"""Initialize julia binary, turning off compiled modules if needed."""
|
146 |
global julia_initialized
|
147 |
-
from julia.core import UnsupportedPythonError
|
148 |
|
149 |
if not julia_initialized:
|
150 |
check_for_conflicting_libraries()
|
151 |
|
152 |
-
|
153 |
-
|
154 |
-
raise ImportError(import_error_string())
|
155 |
julia_project, is_shared = _get_julia_project(julia_project)
|
156 |
_set_julia_project_env(julia_project, is_shared)
|
157 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
158 |
Main = None
|
159 |
try:
|
160 |
from julia import Main as _Main
|
|
|
1 |
"""Functions for initializing the Julia environment and installing deps."""
|
2 |
import sys
|
3 |
+
import subprocess
|
4 |
import warnings
|
5 |
from pathlib import Path
|
6 |
import os
|
|
|
29 |
return juliainfo
|
30 |
|
31 |
|
32 |
+
def _get_julia_env_dir():
|
33 |
+
# Have to manually get env dir:
|
34 |
+
try:
|
35 |
+
julia_env_dir_str = subprocess.run(
|
36 |
+
["julia", "-e using Pkg; print(Pkg.envdir())"], capture_output=True
|
37 |
+
).stdout.decode()
|
38 |
+
except FileNotFoundError:
|
39 |
+
env_path = os.environ["PATH"]
|
40 |
+
raise FileNotFoundError(
|
41 |
+
f"Julia is not installed in your PATH. Please install Julia and add it to your PATH.\n\nCurrent PATH: {env_path}",
|
42 |
+
)
|
43 |
+
return Path(julia_env_dir_str)
|
44 |
|
45 |
|
46 |
def _set_julia_project_env(julia_project, is_shared):
|
47 |
if is_shared:
|
48 |
if is_julia_version_greater_eq(version=(1, 7, 0)):
|
49 |
os.environ["JULIA_PROJECT"] = "@" + str(julia_project)
|
50 |
+
else:
|
51 |
+
julia_env_dir = _get_julia_env_dir()
|
52 |
+
os.environ["JULIA_PROJECT"] = str(julia_env_dir / julia_project)
|
53 |
else:
|
54 |
os.environ["JULIA_PROJECT"] = str(julia_project)
|
55 |
|
|
|
97 |
" so that the Julia environment is properly initialized."
|
98 |
)
|
99 |
|
|
|
|
|
|
|
100 |
|
101 |
def import_error_string(julia_project=None):
|
102 |
s = """
|
|
|
153 |
def init_julia(julia_project=None):
|
154 |
"""Initialize julia binary, turning off compiled modules if needed."""
|
155 |
global julia_initialized
|
|
|
156 |
|
157 |
if not julia_initialized:
|
158 |
check_for_conflicting_libraries()
|
159 |
|
160 |
+
from julia.core import JuliaInfo, UnsupportedPythonError
|
161 |
+
|
|
|
162 |
julia_project, is_shared = _get_julia_project(julia_project)
|
163 |
_set_julia_project_env(julia_project, is_shared)
|
164 |
|
165 |
+
try:
|
166 |
+
info = JuliaInfo.load(julia="julia")
|
167 |
+
except FileNotFoundError:
|
168 |
+
env_path = os.environ["PATH"]
|
169 |
+
raise FileNotFoundError(
|
170 |
+
f"Julia is not installed in your PATH. Please install Julia and add it to your PATH.\n\nCurrent PATH: {env_path}",
|
171 |
+
)
|
172 |
+
|
173 |
+
if not info.is_pycall_built():
|
174 |
+
raise ImportError(import_error_string())
|
175 |
+
|
176 |
Main = None
|
177 |
try:
|
178 |
from julia import Main as _Main
|