New Pkg: how to check if a package is installed?

For instance, if one wants to use OhMyREPL automatically upon startup, the docs say to put

if isdir(Pkg.dir("OhMyREPL"))
    @eval using OhMyREPL
else
    warn("OhMyREPL not installed")
end

into ~/.juliarc.jl. Putting this into ~/.julia/config/startup.jl and adding import Pkg up front results in

┌ Warning: `Pkg.dir(pkgname, paths...)` is deprecated; instead, do `import OhMyREPL; joinpath(dirname(pathof(OhMyREPL)), "..", paths...)`.
└ @ Pkg.API /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.0/Pkg/src/API.jl:454
ERROR: LoadError: MethodError: no method matching joinpath(::Nothing)
Closest candidates are:
  joinpath(!Matched::String, !Matched::String) at path.jl:217
  joinpath(!Matched::AbstractString) at path.jl:199
  joinpath(!Matched::AbstractString, !Matched::AbstractString) at path.jl:226
  ...
Stacktrace:
 [1] stat(::Nothing) at ./stat.jl:107
 [2] isdir(::Nothing) at ./stat.jl:303
 [3] top-level scope at none:0
 [4] include at ./boot.jl:317 [inlined]
 [5] include_relative(::Module, ::String) at ./loading.jl:1038
 [6] include at ./sysimg.jl:29 [inlined]
 [7] include_ifexists at ./client.jl:191 [inlined]
 [8] load_julia_startup() at ./client.jl:288
 [9] exec_options(::Base.JLOptions) at ./logging.jl:312
 [10] _start() at ./client.jl:421
in expression starting at /Users/cbinz/.julia/config/startup.jl:3

So, what’s the canonical way to check whether a package is installed, now?

Pkg.status()

I’d suggest

using Pkg
if in("OhMyREPL",keys(Pkg.installed()))
    @eval using OhMyREPL
else
    @warn("OhMyREPL not installed")
end

4 Likes

Pkg.status() returns “nothing”, can not be programmatically queried. Pkg.installed() gives ugly deprecated warnings.

using Pkg
function isinstalled(pk::AbstractString)
    return pk in [v.name for v in values(Pkg.dependencies())]
end
1 Like