using in try catch block

144 views
Skip to first unread message

David Anthoff

unread,
Jun 27, 2016, 12:12:36 PM6/27/16
to julia...@googlegroups.com

I’m trying to use ``using`` in a try catch block, but that doesn’t seem to be supported.

 

Any other way around this? Essentially I want to load a package, and if the package is not installed, automatically do a ``Pkg.add``.

 

I could of course get a list of all the installed packages and see if the one I need is there, but given the slowness of the package manager, I would prefer to just try to load it first, and only if that fails attempt to ``Pkg.add``.

 

Thanks,

David

 

--

David Anthoff

University of California, Berkeley

 

http://www.david-anthoff.com

 

Tom Breloff

unread,
Jun 27, 2016, 12:20:33 PM6/27/16
to julia-users
Here is what I use for this sort of logic:

function is_installed(pkgstr::AbstractString)
    try
        Pkg.installed(pkgstr) === nothing ? false: true
    catch
        false
    end
end

Yichao Yu

unread,
Jun 27, 2016, 12:48:42 PM6/27/16
to Julia Users


On Jun 27, 2016 12:20 PM, "Tom Breloff" <t...@breloff.com> wrote:
>
> Here is what I use for this sort of logic:
>
> function is_installed(pkgstr::AbstractString)
>     try
>         Pkg.installed(pkgstr) === nothing ? false: true

Dont do this since this will miss package in load path. Just eval a using expression.

David Anthoff

unread,
Jun 27, 2016, 12:54:23 PM6/27/16
to julia...@googlegroups.com

Perfect, that is exactly the kind of thing I was looking for! Thanks, David

Tom Breloff

unread,
Jun 27, 2016, 12:58:19 PM6/27/16
to julia-users
Yichao: is there an alternative "is_installed" definition that would check the load path?  Lets assume I don't actually want to import it, just check.

Yichao Yu

unread,
Jun 27, 2016, 1:02:31 PM6/27/16
to Julia Users


On Jun 27, 2016 12:58 PM, "Tom Breloff" <t...@breloff.com> wrote:
>
> Yichao: is there an alternative "is_installed" definition that would check the load path?  Lets assume I don't actually want to import it, just check.

No.

Cameron McBride

unread,
Jun 27, 2016, 1:44:19 PM6/27/16
to julia-users


On Mon, Jun 27, 2016 at 1:02 PM, Yichao Yu <yyc...@gmail.com> wrote:
>
> On Jun 27, 2016 12:58 PM, "Tom Breloff" <t...@breloff.com> wrote:
> >
> > Yichao: is there an alternative "is_installed" definition that would check the load path?  Lets assume I don't actually want to import it, just check.
>
> No.

Seems like potentially useful function for Pkg module?

Pkg.is_installed(pkgstr)

Or perhaps more useful: is_missing(...)

Although just having several Pkg.add(...) statements for a "setup" step also seems reasonable.

Cameron

Yichao Yu

unread,
Jun 27, 2016, 1:48:41 PM6/27/16
to Julia Users
On Mon, Jun 27, 2016 at 1:43 PM, Cameron McBride
<cameron...@gmail.com> wrote:
>
>
> On Mon, Jun 27, 2016 at 1:02 PM, Yichao Yu <yyc...@gmail.com> wrote:
>>
>> On Jun 27, 2016 12:58 PM, "Tom Breloff" <t...@breloff.com> wrote:
>> >
>> > Yichao: is there an alternative "is_installed" definition that would
>> > check the load path? Lets assume I don't actually want to import it, just
>> > check.
>>
>> No.
>
> Seems like potentially useful function for Pkg module?

https://github.com/JuliaLang/julia/issues/8679

Jeffrey Sarnoff

unread,
Jul 3, 2016, 2:54:21 PM7/3/16
to julia-users
Re: Pkg.is_installed(_), Pkg.is_missing(_)

Pkg.has(_)

Tony Kelman

unread,
Jul 3, 2016, 3:18:40 PM7/3/16
to julia-users
Be aware that doing this kind of thing in combination with precompilation is brittle and error prone (precompiled results will not match the actual status of available packages if that changes). Optional dependencies are better supported by moving code that depends on both other packages into its own package that can declare a dependency on both.
Reply all
Reply to author
Forward
0 new messages