juliohm
juliohm

Reputation: 3779

Julia has some import exception mechanism?

In Python we can do:

try:
    import foo
    # do stuff
except ImportError:
    print("Consider installing package 'foo' for full functionality.")

Is it possible to catch a similar exception in Julia?

Upvotes: 1

Views: 223

Answers (1)

IainDunning
IainDunning

Reputation: 11644

At this time Julia doesn't have an import exception mechanism, and you can't put using inside a try-catch block.

In the comments on this answer, the question is refined to really ask how to do conditional includes. Here is an example of how to do that:

# We are making a module that conditionally includes ImageView
module MyModule
    # Optional plotting features using ImageView
    # The view function is defined in ImageView
    export view  # Re-export ImageView.view (optional)
    try
        require("ImageView")  # Will throw error if ImageView not installed
        global view(args...) = Main.ImageView.view(args...)
    catch
        # Needs global to put it in the module scope, not the catch scope
        global view(args...) = error("ImageView.jl required for drawing!")
        # Alternatively, global view(args...) = nothing
    end
    # ....
end

Upvotes: 4

Related Questions