# How to load packages in a for loop?

**URL:** https://discourse.julialang.org/t/how-to-load-packages-in-a-for-loop/97021
**Category:** General Usage
**Tags:** package, pkg
**Created:** [April 3, 2023, 3:51pm UTC](https://discourse.julialang.org/t/how-to-load-packages-in-a-for-loop/97021 "2023-04-03T15:51:10Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@Hugo](https://discourse.julialang.org/u/Hugo)
#### Post date: [April 3, 2023, 3:51pm UTC](https://discourse.julialang.org/t/how-to-load-packages-in-a-for-loop/97021/1 "2023-04-03T15:51:11Z")

</div>

I have a Julia project that is growing in number of files. To break it into smaller and easier-to-manage files, I want to have a `.jl` where I keep a list of all packages required by the project.

Then, I will use a for loop to iterate over the list. For each package in the list, an `if` statement will check if the package is installed. If `true`, then just load the file with `using`. If `false`, then install the package and load it.

Here, I’m assuming the project environment has been activated. Here’s the code I thought it was going to work.

```julia
# This file is called required_packages.jl.  
#I will include this package into a main.jl file using the include() command
# include('path/to/required_packages.jl')

const required_packages = [
    "DataFrames",
    "Dates",
    "Distributions",
]

# Install package if necessary. Load all packages.
for pkg in required_packages
    if pkg ∉ keys(Pkg.installed())
        Pkg.add(pkg)
    end
    using $pkg
end

```

The error message I get is: `LoadError: TypeError: in using, expected Symbol, got a value of type Expr `. I did try using the function `Symbol()` instead of `$`, but it didn’t work either. All help will be appreciated.

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [April 3, 2023, 4:25pm UTC](https://discourse.julialang.org/t/how-to-load-packages-in-a-for-loop/97021/2 "2023-04-03T16:25:32Z")

</div>

You can do this with `eval`, e.g.

```julia
julia> pkg = :Test
:Test

julia> @eval using $pkg

julia> Test
Test

```

but it’s unclear what problem this solves that is not better solved with some combination of `Project.toml`, `Manifest.toml`, `Pkg.activate`, and `Pkg.instantiate`.

---

<div class="post-metadata">

### Author: ![Hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@Hugo](https://discourse.julialang.org/u/Hugo)
#### Post date: [April 3, 2023, 4:56pm UTC](https://discourse.julialang.org/t/how-to-load-packages-in-a-for-loop/97021/3 "2023-04-03T16:56:52Z")

</div>

Hey, @GunnarFarneback, thanks for your answer. What is the combination of `Project.toml` , `Manifest.toml` , `Pkg.activate` , and `Pkg.instantiate` you would use to solve this problem? For me, using the for loop to check if a package is installed and to load package is the “shortest” and “cleanest” answer.

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [April 3, 2023, 7:09pm UTC](https://discourse.julialang.org/t/how-to-load-packages-in-a-for-loop/97021/4 "2023-04-03T19:09:24Z")

</div>

To be honest I don’t understand the use case but generally speaking I would activate an environment with a saved `Project.toml` rather than repeatedly `Pkg.add`ing packages. If the purpose is just to initialize a number of new environments it seems easier to copy the `Project.toml` to each environment. These tools can’t help with a `using` loop but at this point I would just write out the `using`s.

---

<div class="post-metadata">

### Author: ![Hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@Hugo](https://discourse.julialang.org/u/Hugo)
#### Post date: [April 3, 2023, 9:22pm UTC](https://discourse.julialang.org/t/how-to-load-packages-in-a-for-loop/97021/5 "2023-04-03T21:22:40Z")

</div>

The use case is for a project that I will have to share with people. By doing what I wrote in the code, I hope to make it easier for people to have all the packages the code requires to their environment. If the package is already installed, then all it does is to load the package.

When you write

> I would activate an environment with a saved `Project.toml` rather than repeatedly `Pkg.add` ing packages.

Is there a way to read the packages from the `Project.toml` and automatically install the packages and load them?

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [April 3, 2023, 9:34pm UTC](https://discourse.julialang.org/t/how-to-load-packages-in-a-for-loop/97021/6 "2023-04-03T21:34:35Z")

</div>

If you want to share a project with other people, you should be working in the environment of the project, i.e. activate it, and `Pkg.add` all packages it needs. Then you commit both the `Project.toml` and `Manifest.toml` together with the code.

When other people want to use it, they activate the project and run `Pkg.instantiate`. This ensures not only that they have all the same packages you had, but also the exact same versions.

If your code is designed to be run from scripts, you can start them off with

```julia
using Pkg
Pkg.activate(@ __DIR__ )
Pkg.instantiate

```

for a robustly reproducible workflow, at least if they are using the same Julia version as you.

---

<div class="post-metadata">

### Author: ![andrey2185](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrey2185/32/9889_2.png) [@andrey2185](https://discourse.julialang.org/u/andrey2185)
#### Post date: [April 3, 2023, 9:36pm UTC](https://discourse.julialang.org/t/how-to-load-packages-in-a-for-loop/97021/7 "2023-04-03T21:36:00Z")

</div>

May be?  
`import Pkg; Pkg.activate("."); Pkg.instantiate();`

and look at this:  
[https://pkgdocs.julialang.org/v1/api/#Pkg.instantiate](https://pkgdocs.julialang.org/v1/api/#Pkg.instantiate)

---

<div class="post-metadata">

### Author: ![Hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@Hugo](https://discourse.julialang.org/u/Hugo)
#### Post date: [April 3, 2023, 9:50pm UTC](https://discourse.julialang.org/t/how-to-load-packages-in-a-for-loop/97021/8 "2023-04-03T21:50:48Z")

</div>

Oh, wow! Thanks for this. I’ll certainly look into it.

---

<div class="post-metadata">

### Author: ![Hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@Hugo](https://discourse.julialang.org/u/Hugo)
#### Post date: [April 3, 2023, 10:12pm UTC](https://discourse.julialang.org/t/how-to-load-packages-in-a-for-loop/97021/9 "2023-04-03T22:12:23Z")

</div>

Hey, @andrey2185, thanks for this.
