# Replacement for keys(Pkg.installed()) with Pkg.dependencies()

**URL:** https://discourse.julialang.org/t/replacement-for-keys-pkg-installed-with-pkg-dependencies/94842
**Category:** New to Julia
**Tags:** question
**Created:** [February 18, 2023, 4:19pm UTC](https://discourse.julialang.org/t/replacement-for-keys-pkg-installed-with-pkg-dependencies/94842 "2023-02-18T16:19:23Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![austin-putz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/austin-putz/32/2583_2.png) [@austin-putz](https://discourse.julialang.org/u/austin-putz)
#### Post date: [February 18, 2023, 4:19pm UTC](https://discourse.julialang.org/t/replacement-for-keys-pkg-installed-with-pkg-dependencies/94842/1 "2023-02-18T16:19:23Z")

</div>

I use the current code to pull out installed packages. I want people to just download code for a class, but I’d like to install packages for them. However, `Pkg.installed()` is deprecated and I cannot figure out how to get the vector of package names from `Pkg.dependencies()`

```julia
# load Pkg package (package manager)
using Pkg

# download package if not installed
if !("DataFrames" in keys(Pkg.installed()))
	println("----- DataFrames does not exist and needs to be installed. Please wait...")
	Pkg.add("DataFrames")
else 
	println("----- DataFrames is downloaded already!")
end

```

`Pkg.dependencies().vals` is as far as I got and I’m lost now.

Thanks in advance for any help.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [February 18, 2023, 4:31pm UTC](https://discourse.julialang.org/t/replacement-for-keys-pkg-installed-with-pkg-dependencies/94842/2 "2023-02-18T16:31:25Z")

</div>

I guess the normal way to go this would be to just give them a Project and Manifest file, and have them to `] activate; instantiate`

---

<div class="post-metadata">

### Author: ![austin-putz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/austin-putz/32/2583_2.png) [@austin-putz](https://discourse.julialang.org/u/austin-putz)
#### Post date: [February 18, 2023, 4:36pm UTC](https://discourse.julialang.org/t/replacement-for-keys-pkg-installed-with-pkg-dependencies/94842/3 "2023-02-18T16:36:11Z")

</div>

Okay, thanks. But I would like to just put is at the top of my script. Currently, just re-wrote into a loop such as this and would simply like to plug in a new `Pkg.dependencies()` here.

```julia
# load Pkg package (package manager)
using Pkg

# list packages to install if not installed
my_packages = ["DataFrames", "CategoricalArrays", "GLM", "MixedModels",
"UnicodePlots", "RDatasets", "Statistics", "LinearAlgebra", "Random", 
"Distributions", "Plots", "GLM", "StatsPlots"]

# loop over to install packages
for package in my_packages
	if !(package in keys(Pkg.installed()))
		@info "Installing: " * package
		Pkg.add(package)
	else
		@info "Package " * package * " is installed"
	end
end

```

Thanks.

---

<div class="post-metadata">

### Author: ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)
#### Post date: [February 18, 2023, 4:46pm UTC](https://discourse.julialang.org/t/replacement-for-keys-pkg-installed-with-pkg-dependencies/94842/4 "2023-02-18T16:46:50Z")

</div>

One disadvantage of your approach might be, that you can not control much, which version is installed, so to get closer to the Manifest you could also keep a list of versions and add exactly those versions with `Pkg.add`?

Otherwise you might not be able to ensure that your examples afterwards work with the people you hand this to. The same if the package is installed but a version from 4 years ago.

---

<div class="post-metadata">

### Author: ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)
#### Post date: [February 18, 2023, 4:54pm UTC](https://discourse.julialang.org/t/replacement-for-keys-pkg-installed-with-pkg-dependencies/94842/5 "2023-02-18T16:54:48Z")

</div>

While I would recommend the `Manifest.toml` approach mentioned by @nilshg you can get all package names with

` [p.name for p in values(Pkg.dependencies())]`

but before installing them in the main environment for the users, maybe activate a certain environment to not clutter their main environment? Though then you are even closer to just distributing the Manifest as well.

---

<div class="post-metadata">

### Author: ![austin-putz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/austin-putz/32/2583_2.png) [@austin-putz](https://discourse.julialang.org/u/austin-putz)
#### Post date: [February 18, 2023, 5:42pm UTC](https://discourse.julialang.org/t/replacement-for-keys-pkg-installed-with-pkg-dependencies/94842/6 "2023-02-18T17:42:18Z")

</div>

Okay, then I should look into this approach. I’m not very familiar with the `Manifest.toml` approach. It’s been awhile since I watched some videos on the package manager and how to handle that. I guess I can distribute that on my GitHub for students and they can close the entire directory. I will try testing a few things out and do some research on `Manifest.toml`. Thanks for your help!

---

<div class="post-metadata">

### Author: ![austin-putz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/austin-putz/32/2583_2.png) [@austin-putz](https://discourse.julialang.org/u/austin-putz)
#### Post date: [February 18, 2023, 5:47pm UTC](https://discourse.julialang.org/t/replacement-for-keys-pkg-installed-with-pkg-dependencies/94842/7 "2023-02-18T17:47:05Z")

</div>

I’m sorry, one more thing if you can help. I don’t know how to load that package in my loop by `using`. The string throws off and it’s not a symbol. What do I use? You can see I have it commented out below… Thanks!

```julia
# list packages to install if not installed
my_packages = ["LinearAlgebra", "SparseArrays", "Distributions"]

# list current installed packages
cur_packages_installed = [p.name for p in values(Pkg.dependencies())]

# loop over to install packages
for package in my_packages
	if !(package in cur_packages_installed)
		@info "Installing: " * package
		Pkg.add(package)
	else
		@info "Package " * package * " is installed"
		#using Symbol(package)
	end
end

```

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [February 18, 2023, 6:09pm UTC](https://discourse.julialang.org/t/replacement-for-keys-pkg-installed-with-pkg-dependencies/94842/8 "2023-02-18T18:09:50Z")

</div>

Perhaps @eval in front?

---

<div class="post-metadata">

### Author: ![austin-putz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/austin-putz/32/2583_2.png) [@austin-putz](https://discourse.julialang.org/u/austin-putz)
#### Post date: [February 18, 2023, 6:18pm UTC](https://discourse.julialang.org/t/replacement-for-keys-pkg-installed-with-pkg-dependencies/94842/9 "2023-02-18T18:18:21Z")

</div>

Humm, I can’t get that to work. Thanks.

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [February 18, 2023, 6:36pm UTC](https://discourse.julialang.org/t/replacement-for-keys-pkg-installed-with-pkg-dependencies/94842/10 "2023-02-18T18:36:45Z")

</div>

If you really want to do that, you need to write `@eval using $(Symbol(package))`, but I recommend against doing all this, and using the package manager with a `Project.toml` instead.

---

<div class="post-metadata">

### Author: ![austin-putz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/austin-putz/32/2583_2.png) [@austin-putz](https://discourse.julialang.org/u/austin-putz)
#### Post date: [February 18, 2023, 6:43pm UTC](https://discourse.julialang.org/t/replacement-for-keys-pkg-installed-with-pkg-dependencies/94842/11 "2023-02-18T18:43:08Z")

</div>

This works perfect! Thanks!

Yes, I will do some reading on `Project.toml` but I needed a quick fix so I can post these notes and not have people struggle to install packages since they are all new to Julia.

---

<div class="post-metadata">

### Author: ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)
#### Post date: [February 18, 2023, 6:43pm UTC](https://discourse.julialang.org/t/replacement-for-keys-pkg-installed-with-pkg-dependencies/94842/12 "2023-02-18T18:43:34Z")

</div>

I would also say, take a look at eval, since you want to evaluate the command that you would find in the string `“using Mypackage”), but that makes your code even more complicated.

The Manifest thing is quite easy.

You create a folder o your machine, in that folder you start Julia and do `] activate .`

You will see that the blue/package mode format changes a bit and you have an empty `Project.toml` in that folder. Now (after activating) you add all packages you want.

They will be listed in the Project.toml (and you can even set compass there if you want).

But it also creates the `Manifest.toml`

Send that to the student/collague. They also create a folder (where they also store their code in later) put the manifest in there, they would activate usually as you do, just…

for the very first time they activate, they have to do `] instantiate`. That downloads and precompiles all packages mentioned in the Manifest.

Later if they activate that environment again, they have _exactly the same_ versions of packages again as you specified (set up) the Manifest.

Even better: They also have that if they play around in their general environment with other stuff. Locally in the environment you sent them, they are always back to the package versions you specified (unless they do `] update` of course).

I feel these commands are easier and safer than your idea here.

---

<div class="post-metadata">

### Author: ![austin-putz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/austin-putz/32/2583_2.png) [@austin-putz](https://discourse.julialang.org/u/austin-putz)
#### Post date: [February 18, 2023, 6:45pm UTC](https://discourse.julialang.org/t/replacement-for-keys-pkg-installed-with-pkg-dependencies/94842/13 "2023-02-18T18:45:54Z")

</div>

Got it, this gives me the steps I was going to look for. I’m sure you guys are right, but it’s still more complex for students on their first time or two looking at Julia code. It will suffice for now but I’ll write instructions and move to this model later in the semester as I do like it can fix the package versions which change rapidly right now. Thanks a lot!

---

<div class="post-metadata">

### Author: ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)
#### Post date: [February 18, 2023, 6:56pm UTC](https://discourse.julialang.org/t/replacement-for-keys-pkg-installed-with-pkg-dependencies/94842/14 "2023-02-18T18:56:21Z")

</div>

My version in very short would be: Give them a Pluto notebook. Then they just have to add Pluto – And all I wrote is done by Pluto automatically. Some argue that Pluto notebooks load a bit slow for that reason, but well…

---

<div class="post-metadata">

### Author: ![austin-putz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/austin-putz/32/2583_2.png) [@austin-putz](https://discourse.julialang.org/u/austin-putz)
#### Post date: [February 18, 2023, 6:57pm UTC](https://discourse.julialang.org/t/replacement-for-keys-pkg-installed-with-pkg-dependencies/94842/15 "2023-02-18T18:57:40Z")

</div>

Great to know, thanks! I’ve tried Pluto once or twice.

---

<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: [February 19, 2023, 7:31am UTC](https://discourse.julialang.org/t/replacement-for-keys-pkg-installed-with-pkg-dependencies/94842/16 "2023-02-19T07:31:14Z")

</div>

If you include a `Project.toml` and `Manifest.toml` with your script you could automatically activate and instantiate it using `Pkg` at the start of the script. There are reasons not to do this also, but I would say this is probably a nicer “automatic” solution where your students have the same steps as in your example.

```julia
import Pkg
Pkg.activate(@ __DIR__ ) # Could also just run the script with --project
Pkg.instantiate() # If already instantiated this is quick

using PkgA, PkgB, ...

```

---

<div class="post-metadata">

### Author: ![austin-putz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/austin-putz/32/2583_2.png) [@austin-putz](https://discourse.julialang.org/u/austin-putz)
#### Post date: [February 20, 2023, 1:57pm UTC](https://discourse.julialang.org/t/replacement-for-keys-pkg-installed-with-pkg-dependencies/94842/17 "2023-02-20T13:57:43Z")

</div>

Thanks! I should try this and see how it works with a few students.
