Skip to content

Commit 65301c8

Browse files
tkfstevengj
authored andcommitted
Resolve runtime path (#187)
1 parent 9d1833f commit 65301c8

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,11 @@ Main.eval("sin.(xs)")
112112

113113
If you need a custom setup for `pyjulia`, it must be done *before*
114114
importing any Julia modules. For example, to use the Julia
115-
interpreter at `PATH/TO/MY/CUSTOM/julia`, run:
115+
executable named `custom_julia`, run:
116116

117117
```python
118118
from julia import Julia
119-
j = julia.Julia(jl_runtime_path="PATH/TO/MY/CUSTOM/julia")
119+
jl = julia.Julia(runtime="custom_julia")
120120
```
121121

122122
You can then use, e.g.,

julia/core.py

+31-8
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@
2929
from ctypes import c_char_p as char_p
3030
from ctypes import py_object
3131

32+
try:
33+
from shutil import which
34+
except ImportError:
35+
# For Python < 3.3; it should behave more-or-less similar to
36+
# shutil.which when used with single argument.
37+
from distutils.spawn import find_executable as which
38+
3239
# this is python 3.3 specific
3340
from types import ModuleType, FunctionType
3441

@@ -353,8 +360,8 @@ class Julia(object):
353360
full access to the entire Julia interpreter.
354361
"""
355362

356-
def __init__(self, init_julia=True, jl_runtime_path=None, jl_init_path=None,
357-
debug=False):
363+
def __init__(self, init_julia=True, jl_init_path=None, runtime=None,
364+
jl_runtime_path=None, debug=False):
358365
"""Create a Python object that represents a live Julia interpreter.
359366
360367
Parameters
@@ -365,8 +372,8 @@ def __init__(self, init_julia=True, jl_runtime_path=None, jl_init_path=None,
365372
being called from inside an already running Julia, the flag should
366373
be passed as False so the interpreter isn't re-initialized.
367374
368-
jl_runtime_path : str (optional)
369-
Path to your Julia binary, e.g. "/usr/local/bin/julia"
375+
runtime : str (optional)
376+
Custom Julia binary, e.g. "/usr/local/bin/julia" or "julia-1.0.0".
370377
371378
jl_init_path : str (optional)
372379
Path to give to jl_init relative to which we find sys.so,
@@ -389,13 +396,29 @@ def __init__(self, init_julia=True, jl_runtime_path=None, jl_init_path=None,
389396
self.api = _julia_runtime[0]
390397
return
391398

392-
self._debug() # so that debug message is shown nicely w/ pytest
399+
if jl_runtime_path is not None:
400+
warnings.warn(
401+
"`jl_runtime_path` is deprecated. Please use `runtime`.",
402+
DeprecationWarning)
393403

394-
if init_julia:
395-
if jl_runtime_path:
404+
if runtime is None:
405+
if jl_runtime_path is None:
406+
runtime = "julia"
407+
else:
396408
runtime = jl_runtime_path
409+
else:
410+
if jl_runtime_path is None:
411+
jl_runtime_path = which(runtime)
412+
if jl_runtime_path is None:
413+
raise RuntimeError("Julia runtime {} cannot be found"
414+
.format(runtime))
397415
else:
398-
runtime = 'julia'
416+
raise TypeError(
417+
"Both `runtime` and `jl_runtime_path` are specified.")
418+
419+
self._debug() # so that debug message is shown nicely w/ pytest
420+
421+
if init_julia:
399422
jlinfo = juliainfo(runtime)
400423
JULIA_HOME, libjulia_path, image_file, depsjlexe = jlinfo[:4]
401424
self._debug("pyprogramname =", depsjlexe)

test/test_core.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919

2020
orig_env = os.environ.copy()
21-
julia = Julia(jl_runtime_path=os.getenv("JULIA_EXE"), debug=True)
21+
julia = Julia(runtime=os.getenv("JULIA_EXE"), debug=True)
2222

2323

2424
class JuliaTest(unittest.TestCase):

0 commit comments

Comments
 (0)