Pkg.jl: [feature request] is_installed() function.

Created on 24 Mar 2020  Â·  6Comments  Â·  Source: JuliaLang/Pkg.jl

I think it would be better to add a function "is_installed" to check a certain package is installed or not in Pkg.jl.

The code like:

julia> Pkg.installed()
┌ Warning: Pkg.installed() is deprecated
â”” @ Pkg /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.4/Pkg/src/Pkg.jl:531
Dict{String,VersionNumber} with 1 entry:
  "OhMyREPL" => v"0.5.5"

julia> function is_installed(package)
           for (_, dep) in Pkg.dependencies()
               dep.name == package && return true
           end
           return false
       end
is_installed (generic function with 1 method)

julia> is_installed("PyPlot")
false

julia> is_installed("OhMyREPL")
true

What do you guys think?

Most helpful comment

Whether a package is installed is no longer a globally well-defined concept in Pkg3. What is it that you'd like to know? If doing using X in Main will work? If doing using X in some other module will work? If there's some version of X that has been installed somewhere, even if it cannot be used from Main?

All 6 comments

Whether a package is installed is no longer a globally well-defined concept in Pkg3. What is it that you'd like to know? If doing using X in Main will work? If doing using X in some other module will work? If there's some version of X that has been installed somewhere, even if it cannot be used from Main?

Thank you for your comment.
I want to do lazy loading like:

if is_installed("PyPlot")
    using PyPlot
end

Because, some environments have the package, but other ones does not in my case.
I think Require.jl can do that, but I want to do it by a more simpler way.
https://github.com/JuliaPackaging/Requires.jl

Won't the rest of the code depend on that and error anyway?

I'm sorry that is a very simple example. More correct example is like:

is_plot_aviable = False
if is_installed("PyPlot")
    using PyPlot
    is_plot_available = True
end

// Some other codes

if is_plot_avaiable
    plot()
end

I would do

is_plot_available = false
try
    using PyPlot
    is_plot_available = true
catch
    @warn "PyPlot is not available"
end

@KristofferC Oh. Thank you. I understand that using try-catch is the best way.

Was this page helpful?
0 / 5 - 0 ratings