# How to use Pkg.dependencies() instead of Pkg.installed()

**URL:** <https://discourse.julialang.org/t/how-to-use-pkg-dependencies-instead-of-pkg-installed/36416>\
**Category:** General Usage\
**Tags:** package, package-manager\
**Created:** [March 24, 2020, 5:11am UTC](https://discourse.julialang.org/t/how-to-use-pkg-dependencies-instead-of-pkg-installed/36416 "2020-03-24T05:11:54Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![AtsushiSakai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/atsushisakai/32/6559_2.png) [@AtsushiSakai](https://discourse.julialang.org/u/AtsushiSakai)\
**Post date:** [March 24, 2020, 5:11am UTC](https://discourse.julialang.org/t/how-to-use-pkg-dependencies-instead-of-pkg-installed/36416/1 "2020-03-24T05:11:54Z")

</div>

From Julia v1.4, Pkg.installed() shows deprecation message.  
Using Pkg.dependencies() is recommended instead of Pkg.installed() in this issue:  
[https://github.com/JuliaLang/Pkg.jl/issues/1724](https://github.com/JuliaLang/Pkg.jl/issues/1724)

However, Pkg.dependencies return all packages, not top level packages like Pkg.installed.  
How can I get same results using Pkg.dependencies instead of Pkg.installed?

---

<div class="post-metadata">

**Author:** ![mfh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mfh/32/13787_2.png) [@mfh](https://discourse.julialang.org/u/mfh)\
**Post date:** [March 24, 2020, 6:44am UTC](https://discourse.julialang.org/t/how-to-use-pkg-dependencies-instead-of-pkg-installed/36416/2 "2020-03-24T06:44:14Z")

</div>

Just take a look [at the current implementation of `Pkg.installed()`](https://github.com/JuliaLang/Pkg.jl/blob/master/src/Pkg.jl#L529):

```julia

function installed()
    @warn "Pkg.installed() is deprecated"
    deps = dependencies()
    installs = Dict{String, VersionNumber}()
    for (uuid, dep) in deps
        dep.is_direct_dep || continue
        dep.version === nothing && continue
        installs[dep.name] = dep.version
    end
    return installs
end

```

You just need to look at the field `is_direct_dep` 😄.

---

<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:** [March 24, 2020, 7:01am UTC](https://discourse.julialang.org/t/how-to-use-pkg-dependencies-instead-of-pkg-installed/36416/3 "2020-03-24T07:01:14Z")

</div>

Why do you need `Pkg.installed()`?

---

<div class="post-metadata">

**Author:** ![AtsushiSakai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/atsushisakai/32/6559_2.png) [@AtsushiSakai](https://discourse.julialang.org/u/AtsushiSakai)\
**Post date:** [March 24, 2020, 7:35am UTC](https://discourse.julialang.org/t/how-to-use-pkg-dependencies-instead-of-pkg-installed/36416/4 "2020-03-24T07:35:16Z")

</div>

Oh. Great!!. Your answer is totally what I want!!

---

<div class="post-metadata">

**Author:** ![AtsushiSakai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/atsushisakai/32/6559_2.png) [@AtsushiSakai](https://discourse.julialang.org/u/AtsushiSakai)\
**Post date:** [March 24, 2020, 7:38am UTC](https://discourse.julialang.org/t/how-to-use-pkg-dependencies-instead-of-pkg-installed/36416/5 "2020-03-24T07:38:29Z")

</div>

Sometimes, I want to check a certain package is installed or not in a Julia script for dependency management.

---

<div class="post-metadata">

**Author:** ![mfh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mfh/32/13787_2.png) [@mfh](https://discourse.julialang.org/u/mfh)\
**Post date:** [March 24, 2020, 8:14am UTC](https://discourse.julialang.org/t/how-to-use-pkg-dependencies-instead-of-pkg-installed/36416/6 "2020-03-24T08:14:17Z")

</div>

For such cases Requires is also super helpful by the way:

> **[GitHub - JuliaPackaging/Requires.jl: Lazy code loading for Julia](https://github.com/JuliaPackaging/Requires.jl)**
>
> Lazy code loading for Julia. Contribute to JuliaPackaging/Requires.jl development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [July 8, 2020, 10:51am UTC](https://discourse.julialang.org/t/how-to-use-pkg-dependencies-instead-of-pkg-installed/36416/7 "2020-07-08T10:51:08Z")

</div>

So, what is the best way to check if a package is already installed ? I often want to say “If package xxx is not present, add it”.

In my system:

```julia
haskey(Pkg.installed(), "StatsPlots") # true
haskey(Pkg.dependencies(), "StatsPlots") #false

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 8, 2020, 10:52am UTC](https://discourse.julialang.org/t/how-to-use-pkg-dependencies-instead-of-pkg-installed/36416/8 "2020-07-08T10:52:38Z")

</div>

Use the UUID of StatsPlots as the key instead.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [July 8, 2020, 11:10am UTC](https://discourse.julialang.org/t/how-to-use-pkg-dependencies-instead-of-pkg-installed/36416/9 "2020-07-08T11:10:19Z")

</div>

Thank you, I found it:

```julia
haskey(Pkg.installed(), "StatsPlots") # true
haskey(Pkg.dependencies(), Base.UUID("f3b207a7-027a-5e70-b257-86293d7955fd")) # true

```

However on a usability prospective it is terrible, it is a regression compared to the first line… I need to know what a UUID is, where to find it,…

Why `Pkg.installed(PackageName)` has been deprecated ? Shouldn’t be a higher-level function like it (or better `Pkg.isavailable("PackageName")`, returning directly a bool ) made available ?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 8, 2020, 11:27am UTC](https://discourse.julialang.org/t/how-to-use-pkg-dependencies-instead-of-pkg-installed/36416/10 "2020-07-08T11:27:57Z")

</div>

> [@sylvaticus](#):
>
> However on a usability prospective it is terrible, it is a regression compared to the first line… I need to know what a UUID is, where to find it,…

The first one had the problem in that it only showed packages that were direct dependencies. It’s API was fundamentally incompatible with showing packages in the manifest since you can have multiple packages with the same name there and the key for `installed` was the package name. To have an API that gives information about all dependencies (including recursive) you pretty much must use the UUID as the key since that is what identifies the package.

You can however easily recreate the old API as:

```julia
isinstalled(pkg::String) = any(x -> x.name == pkg && x.is_direct_dep, values(Pkg.dependencies()))

```

Note that this only checks if _a_ package with the name `pkg` is installed, not that it actually is the package you want.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [July 8, 2020, 12:08pm UTC](https://discourse.julialang.org/t/how-to-use-pkg-dependencies-instead-of-pkg-installed/36416/11 "2020-07-08T12:08:35Z")

</div>

But I am interested in just knowing if a package is already available for `using` or not.

If name collision is a (remote, i.e. not possible if you just use the standard repository) possibility one could have just a function that returns the _number_ of available packages by name and then use it as:

```julia
if Pkg.availablecount("pkgName") == 0
  Pkg.add("pkgName")
elseif Pkg.availablecount("pkgName") > 1
  @error "More than one package is available with the given name. Please use UUID"
end

using pkgName

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 8, 2020, 12:12pm UTC](https://discourse.julialang.org/t/how-to-use-pkg-dependencies-instead-of-pkg-installed/36416/12 "2020-07-08T12:12:12Z")

</div>

> [@sylvaticus](#):
>
> But I am interested in just knowing if a package is already available for `using` or not.

You can use the snippet I gave you then.

It’s also possible to just `try` to use it and do something in a `catch`:

```julia
try
    using Foo
catch
   # do something
end

```

---

<div class="post-metadata">

**Author:** ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)\
**Post date:** [March 10, 2021, 7:24pm UTC](https://discourse.julialang.org/t/how-to-use-pkg-dependencies-instead-of-pkg-installed/36416/13 "2021-03-10T19:24:21Z")

</div>

To add to this discussion:  
when I start a program I’d like to enter a vector of packages  
then, automatically/generically  
-check if it’s installed  
-install it if not  
-using all packages in vector

Old way, gives deprec warning

```julia
pk = ["Knet","Colors","Images"]
using Pkg; for p in pk; haskey(Pkg.installed(),p) || Pkg.add(p); end
using Knet, Colors, Images

```

New way using @kristoffer.carlsson function

```julia
isinstalled(pkg::String) = any(x -> x.name == pkg && x.is_direct_dep, values(Pkg.dependencies()))

pk = ["Knet","Colors","Images", "StatsPlots"]
using Pkg; for p in pk; isinstalled(p) || Pkg.add(p); end
using Knet, Colors, Images

```

Also I’d like to replace  
`using Knet, Colors, Images`  
with  
`for p in pk; using(p); end`  
but it doesn’t work bc `pk` is a vector of strings

Adding the function `isinstalled(pkg::String) ` each time is a bit of a pain …

---

<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:** [March 11, 2021, 8:06am UTC](https://discourse.julialang.org/t/how-to-use-pkg-dependencies-instead-of-pkg-installed/36416/14 "2021-03-11T08:06:21Z")

</div>

> [@Albert\_Zevelev](#):
>
> automatically/generically  
> -check if it’s installed  
> -install it if not

I am wondering if you are duplicating the work that is supposed to be done by `Pkg.instantiate`.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [March 11, 2021, 8:44am UTC](https://discourse.julialang.org/t/how-to-use-pkg-dependencies-instead-of-pkg-installed/36416/15 "2021-03-11T08:44:54Z")

</div>

> [@Albert\_Zevelev](#):
>
> Also I’d like to replace  
> `using Knet, Colors, Images`  
> with  
> `for p in pk; using(p); end`

```julia
julia> packages = [:Example,]
1-element Vector{Symbol}:
 :Example

julia> for pkg in packages
           @eval using $pkg
       end

julia> Example.hello("world")
"Hello, world"

```

---

<div class="post-metadata">

**Author:** ![Flock1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/flock1/32/19583_2.png) [@Flock1](https://discourse.julialang.org/u/Flock1)\
**Post date:** [June 20, 2021, 5:58pm UTC](https://discourse.julialang.org/t/how-to-use-pkg-dependencies-instead-of-pkg-installed/36416/16 "2021-06-20T17:58:14Z")

</div>

So is there no one-liner like `Pkg.installed()` now? I ran the for loop and I got the answer but is there any simpler way of getting the installed libraries?

---

<div class="post-metadata">

**Author:** ![mfh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mfh/32/13787_2.png) [@mfh](https://discourse.julialang.org/u/mfh)\
**Post date:** [June 23, 2021, 7:17am UTC](https://discourse.julialang.org/t/how-to-use-pkg-dependencies-instead-of-pkg-installed/36416/17 "2021-06-23T07:17:34Z")

</div>

I’m not aware of any. On the other hand it is a one-liner if you don’t insist on getting exactly the same datastructure as the ancient `installed()`. Like you could just do:

```julia
[dep for dep in dependencies() if dep.is_direct_dep]

```

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [September 7, 2021, 9:30am UTC](https://discourse.julialang.org/t/how-to-use-pkg-dependencies-instead-of-pkg-installed/36416/18 "2021-09-07T09:30:14Z")

</div>

There is the `Pkg.project()` which contains a field named `dependencies` containing a dict with package names and UUID of direct dependencies. So `keys(Pkg.project().dependencies) == keys(Pkg.installed())` if I’m not mistaken, and could for the purpose in this thread probably be used as a one line replacement.

---

<div class="post-metadata">

**Author:** ![Ralf\_Dietrich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralf_dietrich/32/5643_2.png) [@Ralf\_Dietrich](https://discourse.julialang.org/u/Ralf_Dietrich)\
**Post date:** [February 13, 2023, 9:50am UTC](https://discourse.julialang.org/t/how-to-use-pkg-dependencies-instead-of-pkg-installed/36416/19 "2023-02-13T09:50:19Z")

</div>

This code of mine works wonderfully and hopefully solves almost all the issues raised here:

```julia
# Make sure all needed Pkg's are ready to go
neededPackages = [:Gtk4, :Cairo] 
using Pkg;
for neededpackage in neededPackages
    (String(neededpackage) in keys(Pkg.project().dependencies)) || Pkg.add(String(neededpackage))
    @eval using $neededpackage
end

```

---

<div class="post-metadata">

**Author:** ![eotero](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eotero/32/36642_2.png) [@eotero](https://discourse.julialang.org/u/eotero)\
**Post date:** [June 9, 2023, 10:16pm UTC](https://discourse.julialang.org/t/how-to-use-pkg-dependencies-instead-of-pkg-installed/36416/20 "2023-06-09T22:16:04Z")

</div>

Suddenly, you get frustrated with something that should be right away, but at least there is always a way to go. Thank you!

[Next page](https://discourse.julialang.org/t/how-to-use-pkg-dependencies-instead-of-pkg-installed/36416.md?page=2)
