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

  1. I have more than a hundred packages installed. But Pkg.installed() only lists some 40 due to the size of the terminal window. How can I see all installed packages?

  2. Also, how can I have the installed packages printed alphabetically using Pkg.installed()? It’s hard to pick ham from peas.

  3. It takes more than 5 minutes on my machine for Pkg.installed() to return. Is this normal?

  4. Is there a way to see all registered Julia packages on the METADATA git page: https://github.com/JuliaLang/METADATA.jl. The last 277 packages are not see-able on that page.

  5. I use a package which is registered with METADATA. But I don’t use its tagged version, since it’s too old. I use the development version that I checked out from its git repo. However, each time when I Pkg.update(), the tagged version is installed automatically and overrides the version I intend to use. This really drive me nuts as I have to manually Pkg.rm the tagged version. Is there a way to avoid this?

Sorry to be lengthy. Thanks!!

3 Likes

I’ll try and answer these questions, though I’m no expert.

  • Pkg.installed() returns a Dict, so you could pass it to the sort function and also write a loop to see the list a few lines at a time, or just list all the keys in alphabetical order

    showall(sort(collect(keys(Pkg.installed()))))

  • It will probably be O(n) on the number of packages since it’s a file-system operation to look through all packages on disk

  • git clone https://github.com/JuliaLang/METADATA.jl and then view the file locally. The limitation on what you see on the github page is imposed by github.

  • Try Pkg.pin to pin the package to the version you want, or Pkg.checkout to always stay on the latest version.

3 Likes

sort(collect(Pkg.installed())) will print them in sorted order. show(STDOUT, "text/plain", sort(collect(Pkg.installed()))) will print them all, but it would be nice to have something better by default.

Are you using Windows? Julia’s package system currently uses lots of little files in the METADATA repo to store metadata, which is pretty slow on Windows (it was even worse before we switched to libgit in Julia 0.5). At some point we are hoping to switch to a new package metadata system that will eliminate this problem (https://github.com/JuliaLang/Juleps/blob/master/Pkg3.md).

Normally I would recommend looking at the listing at http://pkg.julialang.org/

To see the github listing directly you may have to clone the repo, or do print(readdir(Pkg.dir("METADATA"))) in Julia.

Normally if you do Pkg.checkout("somepackage"), then update() will stay on the master release. Is that not working?

2 Likes

A faster way to access the installed packages can be to read all directories in Pkg.dir()

readdir(Pkg.dir())

Please note that there are some extra directories in it (like .cache, .trash, and METADATA). If you want to print them all alphabetically:

 println.(readdir(Pkg.dir()));

http://pkg.julialang.org/

1 Like

Thanks!

I’m inclined not to clone the repo. I think @stevengj’s method works better for me.

Thanks for the tip!

Yes. It’ll be more convenient if something like show(STDOUT, "text/plain", sort(collect(Pkg.installed()))) can be set as default or at least be able to be opt for.

No. I’m on Ubuntu. I think it’s more or less because my machine is old and was running something else at the same time, though it’s definitely on the scale of minutes rather than seconds.

This works perfectly.

Thanks for the tip!

Yes, it’s indeed quicker than Pkg.installed(), as I usually don’t care much about the version tags. Thanks!

Pkg.dir() only lists the first package directory. This doesn’t work if you have multiple package directories.

1 Like

Thanks for pointing out. I wasn’t aware of this.

filter((x) → typeof(eval(x)) <: Module && x ≠ :Main, names(Main,imported=true))

To answer (1) and (2): Pkg.status()

This was the best option for me on Julia v1.2.0.

6 Likes

How is this done now?

Pkg.installed()
Warning: Pkg.installed() is deprecated
> Pkg.status()
Status `C:\Users\myname\.julia\environments\v1.5\Project.toml` (empty project)
2 Likes

It should work, the above means your project is empty.

If it refers to getting the names of the installed packages in alphabetical order without using the deprecated package, then you can use something like

julia> io = IOBuffer();

julia> using Pkg; Pkg.status(; io)

julia> text = String(take!(io));

julia> lines = split(text, '\n')[2:end-1]
5-element Array{SubString{String},1}:
 "  [336ed68f] CSV v0.8.2"
 "  [a93c6f00] DataFrames v0.22.2"
 "  [59287772] Formatting v0.4.2"
 "  [713c75ef] Franklin v0.10.21"
 "  [38e38edf] GLM v1.3.11"

julia> lines_without_id = [l[14:end] for l in lines]
5-element Array{SubString{String},1}:
 "CSV v0.8.2"
 "DataFrames v0.22.2"
 "Formatting v0.4.2"
 "Franklin v0.10.21"
 "GLM v1.3.11"

this assumes quite a lot about the appearance of the output of Pkg.status. So, not very reliable, but could be useful for scripts.

2 Likes

Less hacky version showing only the direct dependencies:

julia> using Pkg

julia> deps = [pair.second for pair in Pkg.dependencies()];

julia> direct_deps = filter(p -> p.is_direct_dep, deps);

julia> [(x.name, x.version) for x in direct_deps]
10-element Vector{Tuple{String, Any}}:
 ("Compose", v"0.9.2")
 ("DataFrames", v"0.22.3")
 ("Distributions", v"0.23.12")
 ("CategoricalArrays", v"0.8.3")
 ("KernelDensity", v"0.6.2")
 ("Gadfly", v"1.3.1")
 ("Random", nothing)
 ("MCMCChains", v"4.5.0")
 ("Test", nothing)
 ("Turing", v"0.15.1")
6 Likes

Nice, but you still get an ellipsis in the middle, so you don’t see the full list.

That is because Julia will pretty print the long vector. If you really want, you can force it to a string, so that Julia cannot shorten it.

Continuing from the previous code block:

julia> pkgs = [(x.name, x.version) for x in direct_deps];

julia> print(join(pkgs, '\n'))
("Compose", v"0.9.2")
("DataFrames", v"0.22.3")
("Distributions", v"0.23.12")
("CategoricalArrays", v"0.8.3")
("KernelDensity", v"0.6.2")
("Gadfly", v"1.3.1")
("Random", nothing)
("MCMCChains", v"4.5.0")
("Test", nothing)
("Turing", v"0.15.1")

I’ve tested this and it will show the full output even if the size exceeds the size of the terminal.

2 Likes

Rik you’re awesome thank you!

I’m going to clean things up (I have rejoined Julia after a long hiatus and didn’t realize about the environment system, so I’ve made a big mess of my packages). Here’s how I’ve made a list and plan to retrieve it later:

# make list of packages:
using Pkg
deps = [pair.second for pair in Pkg.dependencies()]
direct_deps = filter(p -> p.is_direct_dep, deps)
[(x.name, x.version) for x in direct_deps]
pkg_name_version = [(x.name, x.version) for x in direct_deps]
pkg_list = [x.name for x in direct_deps]

# save list for later:
outfile = "julia-dependencies-2021-04-27.txt"
open(outfile, "w") do f
  for i in pkg_list
    println(f, i)
  end
end


# retrieve list and add packages
using Pkg
infile = "julia-dependencies-2021-04-27.txt"
open(infile, "r") do file
    for line in eachline(file)
        Pkg.add(line)
    end
end
3 Likes

I’m in Julia 1.8.5 and it says Pkg.installed() is deprecated, do you know the current function needed to get a Dict of packages installed?

No idea. I’m on 1.6.7 (LTS) with no plans to upgrade any time soon.