Skip to content

Better error messages from python-jl #300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/julia/python_jl.jl
Original file line number Diff line number Diff line change
@@ -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
13 changes: 3 additions & 10 deletions src/julia/python_jl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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__":
Expand Down