How to test if package is installed

question

#1

What’s the recommended API for testing if a package (which may not be registed) is installed? I have seen two versions floating around:

"somepackage" ∈ keys(Pkg.installed())

and

isdir(Pkg.dir("somepackage"))

The second one feels hackish. First I thought checking Pkg.installed("somepackage") but that can return an error for unregistered, uninstalled packages.

(This is for use in .juliarc.jl, when running different versions.)


#2

The second one is very similar to how Pkg itself does it, so I would not say it’s hackish (unless you believe the approach of Pkg itself to be hackish). If you want to match exactly how Pkg determines if a package is installed, use

isinstalled(pkg::AbstractString) =
    pkg != "METADATA" && pkg != "REQUIRE" && pkg[1] != '.' && Pkg.cd(isdir, pkg)

#3

Pkg doesn’t have the most useful conception of what “installed” means at the moment. What are you trying to accomplish? Modules can be used from anywhere on LOAD_PATH without Pkg knowing anything about them.


#4

basically, something like this in .juliarc:

if (SomePackage is installed) && (VERSION >= v"0.6-")
   eval(quote
           import SomePackage
           SomePackage.dostuff(...)
        end)
end

ie using a package but not causing an error if it is not available.


#5

Can’t you just use a try catch?