MilesCranmer commited on
Commit
3856951
1 Parent(s): fe3d590

Only set shared env JULIA_PROJECT for julia 1.7+

Browse files
Files changed (1) hide show
  1. pysr/julia_helpers.py +47 -26
pysr/julia_helpers.py CHANGED
@@ -6,23 +6,52 @@ import os
6
 
7
  from .version import __version__, __symbolic_regression_jl_version__
8
 
 
9
  julia_initialized = False
10
 
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  def install(julia_project=None, quiet=False): # pragma: no cover
13
  """
14
  Install PyCall.jl and all required dependencies for SymbolicRegression.jl.
15
 
16
  Also updates the local Julia registry.
17
  """
 
 
18
  # Set JULIA_PROJECT so that we install in the pysr environment
19
  julia_project, is_shared = _get_julia_project(julia_project)
20
- if is_shared:
21
- os.environ["JULIA_PROJECT"] = "@" + str(julia_project)
22
- else:
23
- os.environ["JULIA_PROJECT"] = str(julia_project)
24
-
25
- import julia
26
 
27
  julia.install(quiet=quiet)
28
 
@@ -36,7 +65,8 @@ def install(julia_project=None, quiet=False): # pragma: no cover
36
  Main.eval("using Pkg")
37
 
38
  io = "devnull" if quiet else "stderr"
39
- io_arg = f"io={io}" if is_julia_version_greater_eq(Main, "1.6") else ""
 
40
 
41
  # Can't pass IO to Julia call as it evaluates to PyObject, so just directly
42
  # use Main.eval:
@@ -81,9 +111,14 @@ def _get_julia_project(julia_project):
81
  return julia_project, is_shared
82
 
83
 
84
- def is_julia_version_greater_eq(Main, version="1.6"):
85
  """Check if Julia version is greater than specified version."""
86
- return Main.eval(f'VERSION >= v"{version}"')
 
 
 
 
 
87
 
88
 
89
  def check_for_conflicting_libraries(): # pragma: no cover
@@ -104,28 +139,14 @@ def check_for_conflicting_libraries(): # pragma: no cover
104
  def init_julia(julia_project=None):
105
  """Initialize julia binary, turning off compiled modules if needed."""
106
  global julia_initialized
 
107
 
108
  if not julia_initialized:
109
  check_for_conflicting_libraries()
110
 
111
- from julia.core import JuliaInfo, UnsupportedPythonError
112
-
113
  julia_project, is_shared = _get_julia_project(julia_project)
114
- if is_shared:
115
- os.environ["JULIA_PROJECT"] = "@" + str(julia_project)
116
- else:
117
- os.environ["JULIA_PROJECT"] = str(julia_project)
118
-
119
- try:
120
- info = JuliaInfo.load(julia="julia")
121
- except FileNotFoundError:
122
- env_path = os.environ["PATH"]
123
- raise FileNotFoundError(
124
- f"Julia is not installed in your PATH. Please install Julia and add it to your PATH.\n\nCurrent PATH: {env_path}",
125
- )
126
-
127
- if not info.is_pycall_built():
128
- raise ImportError(import_error_string())
129
 
130
  Main = None
131
  try:
 
6
 
7
  from .version import __version__, __symbolic_regression_jl_version__
8
 
9
+ juliainfo = None
10
  julia_initialized = False
11
 
12
 
13
+ def load_juliainfo():
14
+ """Execute julia.core.JuliaInfo.load(), and store as juliainfo."""
15
+ global juliainfo
16
+
17
+ if juliainfo is None:
18
+ from julia.core import JuliaInfo
19
+
20
+ try:
21
+ juliainfo = JuliaInfo.load(julia="julia")
22
+ except FileNotFoundError:
23
+ env_path = os.environ["PATH"]
24
+ raise FileNotFoundError(
25
+ f"Julia is not installed in your PATH. Please install Julia and add it to your PATH.\n\nCurrent PATH: {env_path}",
26
+ )
27
+
28
+ if not juliainfo.is_pycall_built():
29
+ raise ImportError(import_error_string())
30
+
31
+ return juliainfo
32
+
33
+
34
+ def _set_julia_project_env(julia_project, is_shared):
35
+ juliainfo = load_juliainfo()
36
+
37
+ if is_shared:
38
+ if is_julia_version_greater_eq(juliainfo, (1, 7, 0)):
39
+ os.environ["JULIA_PROJECT"] = "@" + str(julia_project)
40
+ else:
41
+ os.environ["JULIA_PROJECT"] = str(julia_project)
42
+
43
+
44
  def install(julia_project=None, quiet=False): # pragma: no cover
45
  """
46
  Install PyCall.jl and all required dependencies for SymbolicRegression.jl.
47
 
48
  Also updates the local Julia registry.
49
  """
50
+ import julia
51
+
52
  # Set JULIA_PROJECT so that we install in the pysr environment
53
  julia_project, is_shared = _get_julia_project(julia_project)
54
+ _set_julia_project_env(julia_project, is_shared)
 
 
 
 
 
55
 
56
  julia.install(quiet=quiet)
57
 
 
65
  Main.eval("using Pkg")
66
 
67
  io = "devnull" if quiet else "stderr"
68
+ juliainfo = load_juliainfo()
69
+ io_arg = f"io={io}" if is_julia_version_greater_eq(juliainfo, (1, 6, 0)) else ""
70
 
71
  # Can't pass IO to Julia call as it evaluates to PyObject, so just directly
72
  # use Main.eval:
 
111
  return julia_project, is_shared
112
 
113
 
114
+ def is_julia_version_greater_eq(juliainfo, version=(1, 6, 0)):
115
  """Check if Julia version is greater than specified version."""
116
+ current_version = (
117
+ juliainfo.version_major,
118
+ juliainfo.version_minor,
119
+ juliainfo.version_patch,
120
+ )
121
+ return current_version >= version
122
 
123
 
124
  def check_for_conflicting_libraries(): # pragma: no cover
 
139
  def init_julia(julia_project=None):
140
  """Initialize julia binary, turning off compiled modules if needed."""
141
  global julia_initialized
142
+ from julia.core import UnsupportedPythonError
143
 
144
  if not julia_initialized:
145
  check_for_conflicting_libraries()
146
 
147
+ juliainfo = load_juliainfo()
 
148
  julia_project, is_shared = _get_julia_project(julia_project)
149
+ _set_julia_project_env(julia_project, is_shared)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
 
151
  Main = None
152
  try: