Skip to content

Commit c45a076

Browse files
tkfstevengj
authored andcommitted
Handle ModuleNotFoundError in pyimport (#579)
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
1 parent 28f75b0 commit c45a076

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/PyCall.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ function pyimport(name::AbstractString)
436436
if ispynull(o)
437437
if pyerr_occurred()
438438
e = PyError("PyImport_ImportModule")
439-
if e.T.o == @pyglobalobjptr(:PyExc_ImportError)
439+
if pyisinstance(e.val, @pyglobalobjptr(:PyExc_ImportError))
440440
# Expand message to help with common user confusions.
441441
msg = """
442442
The Python package $name could not be found by pyimport. Usually this means

test/runtests.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,18 @@ const PyInt = pyversion < v"3" ? Int : Clonglong
321321
@test !ispynull(PyObject(3))
322322
@test ispynull(pydecref(PyObject(3)))
323323

324+
ex = try
325+
pyimport("s p a m")
326+
catch ex
327+
ex
328+
end
329+
@test ex isa PyCall.PyError
330+
@test occursin("could not be found by pyimport", ex.msg)
331+
# Make sure we are testing ModuleNotFoundError here:
332+
if PyCall.pyversion >= v"3.6"
333+
@test pyisinstance(ex.val, pybuiltin("ModuleNotFoundError"))
334+
end
335+
324336
@test !ispynull(pyimport_conda("inspect", "not a conda package"))
325337
import Conda
326338
if PyCall.conda

0 commit comments

Comments
 (0)