MilesCranmer commited on
Commit
bcaffe2
1 Parent(s): b487874

Re-activate env if julia already initialized

Browse files
Files changed (1) hide show
  1. pysr/julia_helpers.py +20 -12
pysr/julia_helpers.py CHANGED
@@ -53,6 +53,11 @@ def _set_julia_project_env(julia_project, is_shared):
53
  else:
54
  os.environ["JULIA_PROJECT"] = str(julia_project)
55
 
 
 
 
 
 
56
 
57
  def install(julia_project=None, quiet=False): # pragma: no cover
58
  """
@@ -68,18 +73,9 @@ def install(julia_project=None, quiet=False): # pragma: no cover
68
  _set_julia_project_env(processed_julia_project, is_shared)
69
 
70
  julia.install(quiet=quiet)
71
- Main = init_julia(julia_project)
 
72
 
73
- Main.eval("using Pkg")
74
-
75
- io = "devnull" if quiet else "stderr"
76
- io_arg = f"io={io}" if is_julia_version_greater_eq(version=(1, 6, 0)) else ""
77
-
78
- # Can't pass IO to Julia call as it evaluates to PyObject, so just directly
79
- # use Main.eval:
80
- Main.eval(
81
- f'Pkg.activate("{_escape_filename(processed_julia_project)}", shared = Bool({int(is_shared)}), {io_arg})'
82
- )
83
  if is_shared:
84
  # Install SymbolicRegression.jl:
85
  _add_sr_to_julia_project(Main, io_arg)
@@ -148,7 +144,7 @@ def _check_for_conflicting_libraries(): # pragma: no cover
148
  )
149
 
150
 
151
- def init_julia(julia_project=None):
152
  """Initialize julia binary, turning off compiled modules if needed."""
153
  global julia_initialized
154
 
@@ -186,6 +182,18 @@ def init_julia(julia_project=None):
186
 
187
  Main = _Main
188
 
 
 
 
 
 
 
 
 
 
 
 
 
189
  julia_initialized = True
190
  return Main
191
 
 
53
  else:
54
  os.environ["JULIA_PROJECT"] = str(julia_project)
55
 
56
+ def _get_io_arg(quiet):
57
+ io = "devnull" if quiet else "stderr"
58
+ io_arg = f"io={io}" if is_julia_version_greater_eq(version=(1, 6, 0)) else ""
59
+ return io_arg
60
+
61
 
62
  def install(julia_project=None, quiet=False): # pragma: no cover
63
  """
 
73
  _set_julia_project_env(processed_julia_project, is_shared)
74
 
75
  julia.install(quiet=quiet)
76
+ Main = init_julia(julia_project, quiet=quiet)
77
+ io_arg = _get_io_arg(quiet)
78
 
 
 
 
 
 
 
 
 
 
 
79
  if is_shared:
80
  # Install SymbolicRegression.jl:
81
  _add_sr_to_julia_project(Main, io_arg)
 
144
  )
145
 
146
 
147
+ def init_julia(julia_project=None, quiet=False):
148
  """Initialize julia binary, turning off compiled modules if needed."""
149
  global julia_initialized
150
 
 
182
 
183
  Main = _Main
184
 
185
+ if julia_initialized:
186
+ Main.eval("using Pkg")
187
+
188
+ io_arg = _get_io_arg(quiet)
189
+ # Can't pass IO to Julia call as it evaluates to PyObject, so just directly
190
+ # use Main.eval:
191
+ Main.eval(
192
+ f'Pkg.activate("{_escape_filename(processed_julia_project)}",'
193
+ f"shared = Bool({int(is_shared)}), "
194
+ f"{io_arg})"
195
+ )
196
+
197
  julia_initialized = True
198
  return Main
199