# Pkg API for getting UUID of another package

**URL:** <https://discourse.julialang.org/t/pkg-api-for-getting-uuid-of-another-package/15061>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 17, 2018, 11:58am UTC](https://discourse.julialang.org/t/pkg-api-for-getting-uuid-of-another-package/15061 "2018-09-17T11:58:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 17, 2018, 11:58am UTC](https://discourse.julialang.org/t/pkg-api-for-getting-uuid-of-another-package/15061/1 "2018-09-17T11:58:54Z")

</div>

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

```julia
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
"31c24e10-a181-5473-b8eb-7969acd0382f"

```

---

<div class="post-metadata">

**Author:** ![Evizero](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evizero/32/10118_2.png) [@Evizero](https://discourse.julialang.org/u/Evizero)\
**Post date:** [September 17, 2018, 12:33pm UTC](https://discourse.julialang.org/t/pkg-api-for-getting-uuid-of-another-package/15061/2 "2018-09-17T12:33:43Z")

</div>

```julia
julia> using Pkg

julia> Pkg.METADATA_compatible_uuid("Distributions")
UUID("31c24e10-a181-5473-b8eb-7969acd0382f")

```

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

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [September 17, 2018, 12:36pm UTC](https://discourse.julialang.org/t/pkg-api-for-getting-uuid-of-another-package/15061/3 "2018-09-17T12:36:33Z")

</div>

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

```julia
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)
               end
           end
           return out
       end
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 [A show command to display package information · Issue #381 · JuliaLang/Pkg.jl · GitHub](https://github.com/JuliaLang/Pkg.jl/issues/381) for the long term solution.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 17, 2018, 12:47pm UTC](https://discourse.julialang.org/t/pkg-api-for-getting-uuid-of-another-package/15061/4 "2018-09-17T12:47:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [September 17, 2018, 12:53pm UTC](https://discourse.julialang.org/t/pkg-api-for-getting-uuid-of-another-package/15061/5 "2018-09-17T12:53:54Z")

</div>

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

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

julia> find_uuid_in_project("JSON")
"682c06a0-de6a-54ab-a142-c8b1cf79cde6"

```

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [February 9, 2023, 12:05pm UTC](https://discourse.julialang.org/t/pkg-api-for-getting-uuid-of-another-package/15061/6 "2023-02-09T12:05:50Z")

</div>

> [@fredrikekre](#):
>
> `function find_uuid(name) [...]`

@fredrikekre  
What would be the actual approach. Your function does not work for me ☹

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [February 26, 2023, 8:37am UTC](https://discourse.julialang.org/t/pkg-api-for-getting-uuid-of-another-package/15061/7 "2023-02-26T08:37:15Z")

</div>

Here is my proposal:

```julia
function find_uuid_in_current_environment(package::AbstractString="Revise")
    UUID = ""
    for (_key, _value) in TOML.tryparsefile(Base.active_project())
        # global UUID # not needed inside a function
        if isa(_value, Dict)
            # println("_value: ", _value)
            println("_key: ", _key)
            if cmp(_key, "deps") == 0
                if haskey(_value, package)
                    UUID = _value[package]
                end
            end
        end
    end
    return package, UUID
end

```

or much more compact:

```julia
TOML.parsefile(Base.active_project())["deps"]["Revise"]

```

(found in old version of package `Tulip.jl`)  
or as function:

```julia
show_uuid(package_name::AbstractString) = TOML.parsefile(Base.active_project())["deps"][package_name]

```
