Skip to content

Commit

Permalink
Handle ModuleNotFoundError in pyimport (#579)
Browse files Browse the repository at this point in the history
Python 3.6 adds `ModuleNotFoundError` which is a subclass of
`ImportError`.  We need to handle all instances of `ImportError`
subclasses as `ImportError` to show instructions about Python
packaging.

https://docs.python.org/3/library/exceptions.html#ModuleNotFoundError
  • Loading branch information
tkf authored and stevengj committed Sep 22, 2018
1 parent 28f75b0 commit c45a076
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/PyCall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ function pyimport(name::AbstractString)
if ispynull(o)
if pyerr_occurred()
e = PyError("PyImport_ImportModule")
if e.T.o == @pyglobalobjptr(:PyExc_ImportError)
if pyisinstance(e.val, @pyglobalobjptr(:PyExc_ImportError))
# Expand message to help with common user confusions.
msg = """
The Python package $name could not be found by pyimport. Usually this means
Expand Down
12 changes: 12 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,18 @@ const PyInt = pyversion < v"3" ? Int : Clonglong
@test !ispynull(PyObject(3))
@test ispynull(pydecref(PyObject(3)))

ex = try
pyimport("s p a m")
catch ex
ex
end
@test ex isa PyCall.PyError
@test occursin("could not be found by pyimport", ex.msg)
# Make sure we are testing ModuleNotFoundError here:
if PyCall.pyversion >= v"3.6"
@test pyisinstance(ex.val, pybuiltin("ModuleNotFoundError"))
end

@test !ispynull(pyimport_conda("inspect", "not a conda package"))
import Conda
if PyCall.conda
Expand Down

0 comments on commit c45a076

Please sign in to comment.