# Pkg & activation

**URL:** <https://discourse.julialang.org/t/pkg-activation/114723>\
**Category:** Package Management\
**Created:** [May 25, 2024, 3:04pm UTC](https://discourse.julialang.org/t/pkg-activation/114723 "2024-05-25T15:04:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [May 25, 2024, 3:04pm UTC](https://discourse.julialang.org/t/pkg-activation/114723/1 "2024-05-25T15:04:46Z")

</div>

OK – I’m a simple `Pkg` user… Here is my problem:

1. I have several Jupyter notebooks in the same directory.
2. In this directory, I have a `Manifest.toml` and a `Project.toml` file. I only want to use these local `*.toml` files in one of my Jupyter notebooks. Reason: one of the notebooks contains some ModelingToolkit code that requires an older version of ModelingToolkit. The other Jupyter notebooks depend on a newer version of MTK.
3. My understanding of `Pkg.jl` and the Environment feature is that…  
– I need to invoke use of a local environment by the `Pkg.activate(...)` command, where, e.g., `Pkg.activate(".")` makes Julia look for the `*.toml` files in the current directory where the Jupyter notebooks are located.  
– If I _do not_ use the `Pkg.activate(..)` command, Julia will use the _global_ environment.
4. Problem: I try to run a Jupyter notebook from this directory _without_ invoking the `Pkg.activate(...)` command. So I expected Julia to use the _global_ environment. However:  
– Julia tells me that I need to _instantiate_ the environment!! [I understand the need for that if Julia actually uses the local environment.]  
– If I change the filenames (local `Manifest.toml` → `_Manifest.toml`; local `Project.toml` → `_Project.toml`), then Julia correctly uses the _global_ `.toml` files.

Is my understanding of _activation_ completely wrong? In other words, is my Point 3 above _wrong_??

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [May 25, 2024, 3:53pm UTC](https://discourse.julialang.org/t/pkg-activation/114723/2 "2024-05-25T15:53:11Z")

</div>

Run `Pkg.status()` to figure iut which environment your Jupyter kernel is running in.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [May 25, 2024, 4:18pm UTC](https://discourse.julialang.org/t/pkg-activation/114723/3 "2024-05-25T16:18:10Z")

</div>

OK – `Pkg.status()` tells me that I’m in the local directory!!

```julia
C:\Users\Bernt\OneDrive\...\Notebooks\Project.toml` (empty project)

```

This is so, even though I did not do `Pkg.activate(".")`!!

- So this implies that (at least when I run Jupyter Notebooks in VSCode), command `Pkg.activate(".")` is not needed – it will always first search for the `*.toml` files locally!
- If there is no `*.toml` files locally, it will go to the global environment and use the `*.toml` files there.

This also implies that I can not use my strategy of having multiple related notebooks in one directory where one of them uses a local environment! (Unless I explicitly specify the global environment in every notebook that do not use the local one?)

Perhaps I misunderstand the role of `Pkg.activate()`… I thought that was meant to specify what is the active environment, and if unspecified, the global environment is used. But instead, it seems to just be to activate the _creation_ of local `*.toml` files??

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [May 25, 2024, 4:56pm UTC](https://discourse.julialang.org/t/pkg-activation/114723/4 "2024-05-25T16:56:57Z")

</div>

You might want to check

1. The Jupyter kernel
2. The notebook

It might be better to explicitly activate an environment in the notebook for reproducibility.

My suspicion is that the environment you installed IJulia with us the environment that the kernel may activate, but it could be the local directory as well.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [May 25, 2024, 6:01pm UTC](https://discourse.julialang.org/t/pkg-activation/114723/5 "2024-05-25T18:01:42Z")

</div>

OK… Here is what I have done for testing things:

1. I have created a new directory, `C:\Users\Bernt\Slett\`. [“Slett” means “Delete”…]

2. In this directory, I have created a `test.jl` file and a `test.ipynb` file using VSCode, both with the following code:

```julia
using Pkg
Pkg.status()

```

1. I also put a `Project.toml` file and a `Manifest.toml` file which a student created for me a while ago.

Now, the directory has the following content:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/2/12f630ffbad876e4ec1a75bd92b11d5a7633f942.png)

1. When running the code, both `test.jl` and `test.ipynb` [Julia release channel] respond with:

```julia
Status `C:\Users\Bernt\.julia\environments\v1.10\Project.toml`
  [336ed68f] CSV v0.10.14
  [a93c6f00] DataFrames v1.6.1
  [82cc6244] DataInterpolations v5.0.0
  [7073ff75] IJulia v1.24.2
  [b964fa9f] LaTeXStrings v1.3.1
  [23fbe1c1] Latexify v0.16.3
  [91a5bcdd] Plots v1.40.4

```

So far, so good: both codes _ignore_ the local `*.toml` files.

1. I then clone the files as `test_1.jl` and `test_1.ipynb`, and in both of the new files, I add the commands:

```julia
Pkg.activate(".")
Pkg.status()

```

1. When I run the `test_1.jl` file, the response after these new lines is:

```julia
Status `C:\Users\Bernt\Project.toml` (empty project)

```

NB: OBSERVE that the files are in directory `C:\Users\Bernt\Slett\`. Probably because the VSCode was in directory `C:\Users\Bernt` by default… It is empty because there were no `*.toml` files in that directory.

1. When I run the `test_1.ipynb` file, the response after these new lines is:

```julia
Project JuliaProjectEnvironment v0.1.0
Status `C:\Users\Bernt\Slett\Project.toml`
→ [0c46a032] DifferentialEquations v7.13.0
   [b964fa9f] LaTeXStrings v1.3.1
→⌃ [961ee093] ModelingToolkit v8.75.0
   [91a5bcdd] Plots v1.40.4
   [37e2e46d] LinearAlgebra
Info Packages marked with → are not downloaded, use `instantiate` to download
Info Packages marked with ⌃ have new versions available and may be upgradable.
  Activating project at `c:\Users\Bernt\Slett`

```

This shows that:  
a. The path of the Jupyter notebook is the same as the path where the notebook was located.  
b. The notebook has found the content of the `*.toml` files created by the student.

1. I then go back to the files `test.jl` and `test_1.ipynb`. These do not contain the `Pkg.activate()` command.

Running the `test.jl` code, the response is:

```julia
Status `C:\Users\Bernt\.julia\environments\v1.10\Project.toml`
  [336ed68f] CSV v0.10.14
  [a93c6f00] DataFrames v1.6.1
  [82cc6244] DataInterpolations v5.0.0
  [7073ff75] IJulia v1.24.2
  [b964fa9f] LaTeXStrings v1.3.1
  [23fbe1c1] Latexify v0.16.3
  [91a5bcdd] Plots v1.40.4

```

No effect of the activation taking place in `test_1.jl` because it created empty `*.toml` files.

1. Finally, I re-run `test.ipynb` file (without an activation statement). Response:

```julia
Status `C:\Users\Bernt\.julia\environments\v1.10\Project.toml`
  [336ed68f] CSV v0.10.14
  [a93c6f00] DataFrames v1.6.1
  [82cc6244] DataInterpolations v5.0.0
  [7073ff75] IJulia v1.24.2
  [b964fa9f] LaTeXStrings v1.3.1
  [23fbe1c1] Latexify v0.16.3
  [91a5bcdd] Plots v1.40.4

```

Again, this is what I _hoped_ would be the result: because I have not used the `Pkg.activate(".")` command, the _global_ environment is used.

1. HOWEVER, in another Jupyter notebook, the notebook uses the local `*.toml` files _even though I have not issued the `Pkg.activate(".")` command_…

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [May 25, 2024, 8:29pm UTC](https://discourse.julialang.org/t/pkg-activation/114723/6 "2024-05-25T20:29:04Z")

</div>

OK – I did a little bit more testing…

1. I created a new file `test_2.ipynb` from `test_1.ipynb` by adding the line `Pkg.instantiate()`.

2. I then run `test.ipynb`, and _now_, `Pkg.status()` responds with:

```julia
Project JuliaProjectEnvironment v0.1.0
Status `C:\Users\Bernt\...\Notebooks\Project.toml`
  [0c46a032] DifferentialEquations v7.13.0
  [b964fa9f] LaTeXStrings v1.3.1
⌃ [961ee093] ModelingToolkit v8.75.0
  [91a5bcdd] Plots v1.40.4
  [37e2e46d] LinearAlgebra
Info Packages marked with ⌃ have new versions available and may be upgradable.

```

What is noteworthy here is:  
a. Now, _after_ I used `Pkg.activate(".")` to _activate_ the local directory, and then did `Pkg.instantiate()` to _instantiate_ the versions of Julia + packages in the `*.toml` files I copied from directory `C:\Users\Bernt\...\Notebooks\` into `C:\Users\Bernt\Slett\`, the `*.toml` files are taken from the directory of the Jupyter notebook.  
b. The _location_ of the file does, however, seem to point to the directory where I copied the `*.toml` files from.

I don’t find this very intuitive. I guess there is a “simple” explanation for why things are like this. Perhaps my failure to find this intuitive is that it works differently from what I was thinking it would do.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [May 25, 2024, 9:40pm UTC](https://discourse.julialang.org/t/pkg-activation/114723/7 "2024-05-25T21:40:03Z")

</div>

OK – I may have figured out how to achieve what I need…

My “problem” is the following:

- If I put `*.toml` files in the same directory as where I have several Jupyter Notebooks, and if I have _activated_ this directory (`Pkg.activate(".")` statement in the Jupyter notebook) as an environment + have _instantiated_ the packages listed in the `*.toml` file ==\> Julia will use the `*.toml` files as _default_ environment when I start the Jupyter notebooks. The consequence is that I can not easily use different environments for different related Notebooks.

- Seems like I can get around this by instead creating the environment in a _subdirectory_ of the directory of the notebooks, e.g., `Pkg.activate("./MyEnv")`. I can then instantiate the `*.toml` files there.

- When I start up one of the notebooks, if I do _not_ issue the `activate` command, Julia will not find `*.toml` files in the directory of the notebook (because they are in the subdirectory). Thus, the global environment will be used.

- If there is a single notebook where I actually want to use the local environment, I simply do `Pkg.activate("./MyEnv")`, and now the local environment will be used.

Anyways, my “problem” stems from the fact that I did not realize that Juila will use a local environment if the `*.toml` files are located in the active directory.

OK – I hope this is correct… if so, it solves my problem.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [May 25, 2024, 9:53pm UTC](https://discourse.julialang.org/t/pkg-activation/114723/8 "2024-05-25T21:53:59Z")

</div>

See the documentation here:  
[https://julialang.github.io/IJulia.jl/stable/manual/installation/](https://julialang.github.io/IJulia.jl/stable/manual/installation/)

---

<div class="post-metadata">

**Author:** ![danielwe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielwe/32/35657_2.png) [@danielwe](https://discourse.julialang.org/u/danielwe)\
**Post date:** [May 25, 2024, 10:40pm UTC](https://discourse.julialang.org/t/pkg-activation/114723/9 "2024-05-25T22:40:41Z")

</div>

> [@mkitti](#):
>
> My suspicion is that the environment you installed IJulia with us the environment that the kernel may activate, but it could be the local directory as well.

With the default configuration, IJulia launches julia with `--project=@.`, which activates any local environment that exists within the current folder or any of its parents. See [Using IJulia · IJulia](https://julialang.github.io/IJulia.jl/stable/manual/usage/#Julia-projects)
