From f3d724db92ef1a670cdc61d11a21441c1dfd7d28 Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Sat, 27 Apr 2019 10:12:29 -0700 Subject: [PATCH] Better error messages from python-jl --- src/julia/python_jl.jl | 49 ++++++++++++++++++++++++++++++++++++++++++ src/julia/python_jl.py | 13 +++-------- 2 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 src/julia/python_jl.jl diff --git a/src/julia/python_jl.jl b/src/julia/python_jl.jl new file mode 100644 index 00000000..58117e48 --- /dev/null +++ b/src/julia/python_jl.jl @@ -0,0 +1,49 @@ +if VERSION < v"0.7-" + error("Unsupported Julia version: $VERSION") +end + +import PyCall + +@debug "Trying to import Python module `julia`..." +try + PyCall.pyimport("julia") +catch err + if PyCall.pyisinstance(err.val, PyCall.pybuiltin("ImportError")) + @error """ +Python module `julia` cannot be imported. It is likely that you are +installing PyJulia in the Python environment that is not used by PyCall. Note +that `python-jl` program needs PyJulia to be installed in the Python environment +used by PyCall. + +PyCall is configured to use Python executable: + $(PyCall.pyprogramname) + +See PyCall documentation: + https://github.com/JuliaPy/PyCall.jl#specifying-the-python-version + """ + exit(1) + end + rethrow() +end + +@debug "Trying to import Python module `julia.pseudo_python_cli`..." +let cli = try + PyCall.pyimport("julia.pseudo_python_cli") + catch err + if PyCall.pyisinstance(err.val, PyCall.pybuiltin("ImportError")) + @error "Incompatible version of PyJulia is installed for PyCall." + exit(1) + end + rethrow() + end, + main = try + cli.main + catch + cli[:main] + end, + code = main(ARGS) + + if code isa Integer + exit(code) + end +end diff --git a/src/julia/python_jl.py b/src/julia/python_jl.py index c07f3f22..4e3dcad5 100644 --- a/src/julia/python_jl.py +++ b/src/julia/python_jl.py @@ -21,6 +21,7 @@ from __future__ import absolute_import, print_function +import os import sys from .pseudo_python_cli import ARGUMENT_HELP, make_parser, parse_args_with @@ -33,15 +34,7 @@ """ ) -script_jl = """ -import PyCall - -let code = PyCall.pyimport("julia.pseudo_python_cli")[:main](ARGS) - if code isa Integer - exit(code) - end -end -""" +script_jl = os.path.join(os.path.dirname(os.path.realpath(__file__)), "python_jl.jl") def remove_julia_options(args): @@ -111,7 +104,7 @@ def main(args=None): args = sys.argv[1:] ns, unused_args = parse_pyjl_args(args) julia = ns.julia - execprog([julia, "-e", script_jl, "--"] + unused_args) + execprog([julia, script_jl, "--"] + unused_args) if __name__ == "__main__":