MilesCranmer commited on
Commit
6e2fc47
1 Parent(s): 3ac3213

Add workaround for statically-linked libpython

Browse files
Files changed (2) hide show
  1. pysr/__init__.py +10 -1
  2. pysr/sr.py +50 -13
pysr/__init__.py CHANGED
@@ -1,4 +1,13 @@
1
- from .sr import pysr, get_hof, best, best_tex, best_callable, best_row, install
 
 
 
 
 
 
 
 
 
2
  from .feynman_problems import Problem, FeynmanProblem
3
  from .export_jax import sympy2jax
4
  from .export_torch import sympy2torch
 
1
+ from .sr import (
2
+ pysr,
3
+ get_hof,
4
+ best,
5
+ best_tex,
6
+ best_callable,
7
+ best_row,
8
+ install,
9
+ silence_julia_warning,
10
+ )
11
  from .feynman_problems import Problem, FeynmanProblem
12
  from .export_jax import sympy2jax
13
  from .export_torch import sympy2torch
pysr/sr.py CHANGED
@@ -12,6 +12,8 @@ from datetime import datetime
12
  import warnings
13
  from multiprocessing import cpu_count
14
 
 
 
15
 
16
  def install(julia_project=None):
17
  import julia
@@ -20,6 +22,7 @@ def install(julia_project=None):
20
 
21
  julia_project = _get_julia_project(julia_project)
22
 
 
23
  from julia import Pkg
24
 
25
  Pkg.activate(f"{_escape_filename(julia_project)}")
@@ -293,19 +296,7 @@ def pysr(
293
  if multithreading:
294
  os.environ["JULIA_NUM_THREADS"] = str(procs)
295
 
296
- from julia.core import JuliaInfo
297
-
298
- info = JuliaInfo.load(julia="julia")
299
- if not info.is_pycall_built():
300
- raise ImportError(
301
- """
302
- Required dependencies are not installed or built. Run the following code in the Python REPL:
303
-
304
- >>> import pysr
305
- >>> pysr.install()"""
306
- )
307
-
308
- from julia import Main
309
 
310
  buffer_available = "buffer" in sys.stdout.__dir__()
311
 
@@ -964,3 +955,49 @@ def _get_julia_project(julia_project):
964
  if julia_project is None:
965
  return pkg_directory
966
  return Path(julia_project)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  import warnings
13
  from multiprocessing import cpu_count
14
 
15
+ is_julia_warning_silenced = False
16
+
17
 
18
  def install(julia_project=None):
19
  import julia
 
22
 
23
  julia_project = _get_julia_project(julia_project)
24
 
25
+ init_julia()
26
  from julia import Pkg
27
 
28
  Pkg.activate(f"{_escape_filename(julia_project)}")
 
296
  if multithreading:
297
  os.environ["JULIA_NUM_THREADS"] = str(procs)
298
 
299
+ Main = init_julia()
 
 
 
 
 
 
 
 
 
 
 
 
300
 
301
  buffer_available = "buffer" in sys.stdout.__dir__()
302
 
 
955
  if julia_project is None:
956
  return pkg_directory
957
  return Path(julia_project)
958
+
959
+
960
+ def silence_julia_warning():
961
+ global is_julia_warning_silenced
962
+ is_julia_warning_silenced = True
963
+
964
+
965
+ def init_julia():
966
+ """Initialize julia binary, turning off compiled modules if needed."""
967
+ global is_julia_warning_silenced
968
+ from julia.core import JuliaInfo, UnsupportedPythonError
969
+
970
+ info = JuliaInfo.load(julia="julia")
971
+ if not info.is_pycall_built():
972
+ raise ImportError(
973
+ """
974
+ Required dependencies are not installed or built. Run the following code in the Python REPL:
975
+
976
+ >>> import pysr
977
+ >>> pysr.install()"""
978
+ )
979
+
980
+ Main = None
981
+ try:
982
+ from julia import Main as _Main
983
+
984
+ Main = _Main
985
+ except UnsupportedPythonError:
986
+ if not is_julia_warning_silenced:
987
+ warnings.warn(
988
+ """
989
+ Your Python version is statically linked to libpython. For example, this could be the python included with conda, or maybe your system's built-in python.
990
+ This will still work, but the precompilation cache for Julia will be turned off, which may result in slower startup times on the initial pysr() call.
991
+
992
+ To install a Python version that is dynamically linked to libpython, pyenv is recommended (https://github.com/pyenv/pyenv).
993
+
994
+ To silence this warning, you can run pysr.silence_julia_warning() after importing pysr."""
995
+ )
996
+ from julia.core import Julia
997
+
998
+ jl = Julia(compiled_modules=False)
999
+ from julia import Main as _Main
1000
+
1001
+ Main = _Main
1002
+
1003
+ return Main