@@ -33,7 +33,9 @@ def _get_julia_env_dir():
33
33
# Have to manually get env dir:
34
34
try :
35
35
julia_env_dir_str = subprocess .run (
36
- ["julia" , "-e using Pkg; print(Pkg.envdir())" ], capture_output = True
36
+ ["julia" , "-e using Pkg; print(Pkg.envdir())" ],
37
+ capture_output = True ,
38
+ env = os .environ ,
37
39
).stdout .decode ()
38
40
except FileNotFoundError :
39
41
env_path = os .environ ["PATH" ]
@@ -54,6 +56,12 @@ def _set_julia_project_env(julia_project, is_shared):
54
56
os .environ ["JULIA_PROJECT" ] = str (julia_project )
55
57
56
58
59
+ def _get_io_arg (quiet ):
60
+ io = "devnull" if quiet else "stderr"
61
+ io_arg = f"io={ io } " if is_julia_version_greater_eq (version = (1 , 6 , 0 )) else ""
62
+ return io_arg
63
+
64
+
57
65
def install (julia_project = None , quiet = False ): # pragma: no cover
58
66
"""
59
67
Install PyCall.jl and all required dependencies for SymbolicRegression.jl.
@@ -64,28 +72,13 @@ def install(julia_project=None, quiet=False): # pragma: no cover
64
72
65
73
_version_assertion ()
66
74
# Set JULIA_PROJECT so that we install in the pysr environment
67
- julia_project , is_shared = _process_julia_project (julia_project )
68
- _set_julia_project_env (julia_project , is_shared )
75
+ processed_julia_project , is_shared = _process_julia_project (julia_project )
76
+ _set_julia_project_env (processed_julia_project , is_shared )
69
77
70
78
julia .install (quiet = quiet )
79
+ Main = init_julia (julia_project , quiet = quiet )
80
+ io_arg = _get_io_arg (quiet )
71
81
72
- if is_shared :
73
- # is_shared is only true if the julia_project arg was None
74
- # See _process_julia_project
75
- Main = init_julia (None )
76
- else :
77
- Main = init_julia (julia_project )
78
-
79
- Main .eval ("using Pkg" )
80
-
81
- io = "devnull" if quiet else "stderr"
82
- io_arg = f"io={ io } " if is_julia_version_greater_eq (version = (1 , 6 , 0 )) else ""
83
-
84
- # Can't pass IO to Julia call as it evaluates to PyObject, so just directly
85
- # use Main.eval:
86
- Main .eval (
87
- f'Pkg.activate("{ _escape_filename (julia_project )} ", shared = Bool({ int (is_shared )} ), { io_arg } )'
88
- )
89
82
if is_shared :
90
83
# Install SymbolicRegression.jl:
91
84
_add_sr_to_julia_project (Main , io_arg )
@@ -117,11 +110,14 @@ def _import_error_string(julia_project=None):
117
110
def _process_julia_project (julia_project ):
118
111
if julia_project is None :
119
112
is_shared = True
120
- julia_project = f"pysr-{ __version__ } "
113
+ processed_julia_project = f"pysr-{ __version__ } "
114
+ elif julia_project [0 ] == "@" :
115
+ is_shared = True
116
+ processed_julia_project = julia_project [1 :]
121
117
else :
122
118
is_shared = False
123
- julia_project = Path (julia_project )
124
- return julia_project , is_shared
119
+ processed_julia_project = Path (julia_project )
120
+ return processed_julia_project , is_shared
125
121
126
122
127
123
def is_julia_version_greater_eq (juliainfo = None , version = (1 , 6 , 0 )):
@@ -151,7 +147,7 @@ def _check_for_conflicting_libraries(): # pragma: no cover
151
147
)
152
148
153
149
154
- def init_julia (julia_project = None ):
150
+ def init_julia (julia_project = None , quiet = False ):
155
151
"""Initialize julia binary, turning off compiled modules if needed."""
156
152
global julia_initialized
157
153
@@ -161,8 +157,8 @@ def init_julia(julia_project=None):
161
157
from julia .core import JuliaInfo , UnsupportedPythonError
162
158
163
159
_version_assertion ()
164
- julia_project , is_shared = _process_julia_project (julia_project )
165
- _set_julia_project_env (julia_project , is_shared )
160
+ processed_julia_project , is_shared = _process_julia_project (julia_project )
161
+ _set_julia_project_env (processed_julia_project , is_shared )
166
162
167
163
try :
168
164
info = JuliaInfo .load (julia = "julia" )
@@ -189,11 +185,24 @@ def init_julia(julia_project=None):
189
185
190
186
Main = _Main
191
187
188
+ if julia_initialized :
189
+ Main .eval ("using Pkg" )
190
+
191
+ io_arg = _get_io_arg (quiet )
192
+ # Can't pass IO to Julia call as it evaluates to PyObject, so just directly
193
+ # use Main.eval:
194
+ Main .eval (
195
+ f'Pkg.activate("{ _escape_filename (processed_julia_project )} ",'
196
+ f"shared = Bool({ int (is_shared )} ), "
197
+ f"{ io_arg } )"
198
+ )
199
+
192
200
julia_initialized = True
193
201
return Main
194
202
195
203
196
204
def _add_sr_to_julia_project (Main , io_arg ):
205
+ Main .eval ("using Pkg" )
197
206
Main .sr_spec = Main .PackageSpec (
198
207
name = "SymbolicRegression" ,
199
208
url = "https://github.com/MilesCranmer/SymbolicRegression.jl" ,
0 commit comments