Pkg API for getting UUID of another package

How can I look up the UUID of a package by name in the current project using the Pkg API?

julia> import Pkg; Pkg.status()
    Status `~/.julia/environments/v1.1/Project.toml`
  [6e4b80f9] BenchmarkTools v0.4.1
  [31c24e10] Distributions v0.16.4

julia> Pkg.gimme_UUID("Distributions") # hypothetical function
julia> using Pkg

julia> Pkg.METADATA_compatible_uuid("Distributions")

(note that I think this is just temporary until METADATA is a thing of the past)

Evizeros version works as long as we have METADATA as the truth of the General registry. Here is a more “correct” version:

julia> function find_uuid(name)
           packages = merge!((Pkg.TOML.parsefile(joinpath(x, "Registry.toml"))["packages"] for x in Pkg.Types.registries())...)
           out = Pair{String,String}[]
           for (k, v) in packages
               if get(v, "name", nothing) == name
                   push!(out, name => k)
           return out
find_uuid (generic function with 1 method)

julia> find_uuid("JSON")
1-element Array{Pair{String,String},1}:
 "JSON" => "682c06a0-de6a-54ab-a142-c8b1cf79cde6"

See also for the long term solution.

1 Like

Thank you and @evizero for the solutions. Sorry I wasn’t specific enough: I want to UUID of a local, unregistered package. Eg just after running pkg> generate. I guess the above issue will be the solution.

If you have it installed as a deps in your project:

julia> function find_uuid_in_project(name)
           get(Pkg.Types.EnvCache().project["deps"], name, nothing)
find_uuid_in_project (generic function with 1 method)

julia> find_uuid_in_project("JSON")
1 Like