How to see all installed packages? + a few other `Pkg` related questions

You can explore stuff like this if you like:

using Pkg

p = Pkg.project()
d = Pkg.dependencies()

for k in p.dependencies
    pk = d[last(k)]
    println(pk.name, " ", pk.version)
end

Not sure if that’s an “official” way or not.

2 Likes

Pkg.dependencies() is official but not Pkg.project but everything is inside Pkg.dependencies() anyway so you don’t really need thatl

1 Like

@kristoffer.carlsson thank you very much, but when I do Pkg.dependencies() I get packages I have not installed on the current version of Julia (1.8.5 just today). It lists packages I’ve installed in the past. I don’t know how to write an up to date replacement for the following. Can you help? Or maybe there is a link somewhere showing how to do this?

# Check if BenchmarkTools is installed
if !("BenchmarkTools" in keys(Pkg.installed()))
Pkg.add("BenchmarkTools")
end

Oh nvm, looks like all I needed as to replace installed with dependencies… sorry.

Note that Pkg.dependencies() have UUIDs as their keys. But you can do your BenchmarkTools query like

julia> any(x -> x.name == "BenchmarkTools", values(Pkg.dependencies()))
true

In other words: depreciating Pkg.installed() makes our code more complicated and harder to read. Why was this done in the first place?

1 Like

You mean deprecating Pkg.installed? The reason was that you can have multiple packages with the same name in an environment now, so having a dictionary with the package names as the keys means that you could have collisions. The UUIDs are unique so that’s what was chosen as the dict key.

If we restricted the returned packages as those that are in the project then the names would be unique though. So there could be a Pkg.direct_dependencies() or something with names as the key to the dictionary.

And for the record, I don’t think Pkg.dependencies() is super elegant, it might be better just to parse the TOML file.

julia> using TOML

julia> d = TOML.parsefile(Base.active_project());

julia> haskey(d["deps"], "BenchmarkTools")
true
2 Likes

Because I’m a big meanie and I wanted to make peoples lives more difficult.

Just kidding of course. But you can assume that if something was deprecated, there was a good reason for it. In this case: the old Pkg.installed() function doesn’t really make sense in the world of Pkg3 (i.e. post 1.0) where packages are no longer uniquely identified by name.

2 Likes