Optional import mechanism

286 views
Skip to first unread message

Júlio Hoffimann

unread,
Aug 19, 2014, 5:09:50 PM8/19/14
to julia...@googlegroups.com
Dear all,

Consider the following code:

try
    require("ImageView")
    global view = ImageView.view
catch err
    @show err
    # fallback to nothing
    global view = view(args...; kargs...) = (nothing, nothing)
end

I have ImageView installed, but the catch block is executed with err => UndefVarError(:ImageView). The symbol ImageView isn't visible somehow. How to fix the error?

Best,
Júlio.

Kevin Squire

unread,
Aug 19, 2014, 5:25:37 PM8/19/14
to julia...@googlegroups.com
What happens if you use import instead is require?

Júlio Hoffimann

unread,
Aug 19, 2014, 9:00:39 PM8/19/14
to julia...@googlegroups.com
Hi Kevin,

What happens if you use import instead is require?

ERROR: error compiling anonymous: unsupported or misplaced expression import in function anonymous

Júlio.

Joey Huchette

unread,
Aug 19, 2014, 9:23:55 PM8/19/14
to julia...@googlegroups.com

It’s a bit ugly, but this should work:

try
    eval(Expr(:import, :ImageView))
    global view = ImageView.view
catch err
    @show err
    # fallback to nothing
    global view = view(args...; kargs...) = (nothing, nothing)
end

Júlio Hoffimann

unread,
Aug 19, 2014, 9:38:27 PM8/19/14
to julia...@googlegroups.com
Hi Joey,

It’s a bit ugly, but this should work:

try
    eval(Expr(:import, :ImageView))
    global view = ImageView.view
catch err
    @show err
    # fallback to nothing
    global view = view(args...; kargs...) = (nothing, nothing)
end

Unfortunately it doesn't work:

err => UndefVarError(:ImageView)
err => ErrorException("invalid redefinition of constant view")

Júlio.

Miles Lubin

unread,
Aug 19, 2014, 10:45:04 PM8/19/14
to julia...@googlegroups.com
This may do what you want (snippet from JuMP):

if isdir(Pkg.dir("ArrayViews"))
   
eval(Expr(:import,:ArrayViews))
   
const subarr = ArrayViews.view
else
   
const subarr = Base.sub
end

Tim Holy

unread,
Aug 20, 2014, 4:38:32 AM8/20/14
to julia...@googlegroups.com
Doesn't just adding Main in front solve the problem?

view = Main.ImageView.view

--Tim

Júlio Hoffimann

unread,
Aug 20, 2014, 11:52:38 AM8/20/14
to julia...@googlegroups.com
Doesn't just adding Main in front solve the problem?

    view = Main.ImageView.view

--Tim

Thanks Tim! It did the trick. The correct solution: https://github.com/juliohm/ImageQuilting.jl/blob/master/src/imquilt.jl#L22-L28

It's not ideal, but it works. It would be much nicer if `using ImageView` raised an ImportError exception or something rather than relying on the require() function.

I invite everyone to give it a try with a fresh Julia build: https://github.com/juliohm/ImageQuilting.jl

Best,
Júlio.

Iain Dunning

unread,
Aug 20, 2014, 2:16:33 PM8/20/14
to julia...@googlegroups.com
Soon you'll be able to use https://github.com/JuliaLang/julia/pull/6884
and everything can be much nicer :)

David van Leeuwen

unread,
Dec 22, 2014, 5:54:50 AM12/22/14
to julia...@googlegroups.com
Hello, 

I've read some of the discussion related to conditional / optional module loading, and I am not sure what the status of https://github.com/JuliaLang/julia/pull/6884---I guess it still is open for merge.  I'm also not sure if it would address the following.  

I'm in the situation where a (small) part of a package would depend on a plotting package, e.g., Winston, but I would like to be able to use most of the functionality on a run-time system without interactive graphics capabilities.   

A top level `using Winston` now bails out in a command-line-only environment, because there is no DISPLAY environment variable that Tk needs that is required by Winston. 

A load-at-runtime by including the `using Winston` in a clever `eval`-way inside a plotting function, such that an attempt to load the package is made only after first call to my plotting function is further complicated by the fact that my package needs to import the `plot` function from Winston if I want to call this plotting function `plot(::MyType)`. 

Any ideas?

Thanks, 

---david

Tim Holy

unread,
Dec 22, 2014, 6:07:17 AM12/22/14
to julia...@googlegroups.com
For this specific case, if you're on Linux, from the command line try
xvfb-run julia -e 'include("my_plotting_script")'
You'll need xvfb installed, of course.

--Tim

David van Leeuwen

unread,
Dec 22, 2014, 6:54:41 AM12/22/14
to julia...@googlegroups.com
Thanks, Tim, this indeed solves my immediate problem!

---david
Reply all
Reply to author
Forward
0 new messages