# How to run (activate?) a Jupyter Notebook in a specific environment

**URL:** https://discourse.julialang.org/t/how-to-run-activate-a-jupyter-notebook-in-a-specific-environment/63841
**Category:** Jupyter-Notebook
**Created:** [June 30, 2021, 3:02pm UTC](https://discourse.julialang.org/t/how-to-run-activate-a-jupyter-notebook-in-a-specific-environment/63841 "2021-06-30T15:02:45Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)
#### Post date: [June 30, 2021, 3:02pm UTC](https://discourse.julialang.org/t/how-to-run-activate-a-jupyter-notebook-in-a-specific-environment/63841/1 "2021-06-30T15:02:45Z")

</div>

I am trying to run a Jupyter notebook (and a project in general) in a specific environment with only a limited number of packages.

I have created a folder. In my terminal, I navigated to this folder, started julia, and ran `]activate .`. I then added a few packages `]add DifferentialEquations, Plots`. Next I started jupyter, and created a Jupyter notebook in the same folder (in which there now is a Manifest.toml and a Project.toml file). I start the Jupyter notebook. However, this Jupyter notebook have access to a lot of other packages more than jus DifferentialEquations and Plots (packages which I have added to my main environment).

I have tried running `using Pkg; Pkg.activate()` in the Jupyter notebook, but didn’t work.

Exactly how would I go about to make this happen?

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [June 30, 2021, 3:06pm UTC](https://discourse.julialang.org/t/how-to-run-activate-a-jupyter-notebook-in-a-specific-environment/63841/2 "2021-06-30T15:06:01Z")

</div>

What does `pwd()` give you? Maybe you aren’t in the correct directory.

---

<div class="post-metadata">

### Author: ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)
#### Post date: [June 30, 2021, 3:07pm UTC](https://discourse.julialang.org/t/how-to-run-activate-a-jupyter-notebook-in-a-specific-environment/63841/3 "2021-06-30T15:07:20Z")

</div>

If I run `pwd()` in the Jupyter notebook it returns the path to the place where the jupyter notebook and the 2 toml files are (if I understood the question right?).

---

<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: [June 30, 2021, 3:34pm UTC](https://discourse.julialang.org/t/how-to-run-activate-a-jupyter-notebook-in-a-specific-environment/63841/4 "2021-06-30T15:34:09Z")

</div>

> [@Torkel](#):
>
> However, this Jupyter notebook have access to a lot of other packages more than jus DifferentialEquations and Plots (packages which I have added to my main environment).

This is expected. The load path is a stack, and the active project is just the top-most entry. You can inspect the stack with `Base.load_path()`.

---

<div class="post-metadata">

### Author: ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)
#### Post date: [June 30, 2021, 3:42pm UTC](https://discourse.julialang.org/t/how-to-run-activate-a-jupyter-notebook-in-a-specific-environment/63841/5 "2021-06-30T15:42:09Z")

</div>

I see, yes if I do `Base.load_path()` things points somewhere else.

However, how would I do to set a more limited scope? Typically I have just run everything in the main one, but if I understand things right the recommended approach is to have a specific environment for every project (?). The idea is to prepare some files and code, so that someone else then could re-run the project in the same environment using that file.

(also, the main environment is a mess, and I can’t really update it anymore because various incompatibilities starts to appear)

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [June 30, 2021, 3:52pm UTC](https://discourse.julialang.org/t/how-to-run-activate-a-jupyter-notebook-in-a-specific-environment/63841/6 "2021-06-30T15:52:44Z")

</div>

`push!(empty!(LOAD_PATH), "@")` should do it.

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [June 30, 2021, 3:54pm UTC](https://discourse.julialang.org/t/how-to-run-activate-a-jupyter-notebook-in-a-specific-environment/63841/7 "2021-06-30T15:54:06Z")

</div>

You can control the load path with the `JULIA_LOAD_PATH` environment variable. See the following pieces of documentation:

- [Code Loading · The Julia Language](https://docs.julialang.org/en/v1/manual/code-loading/#Environment-stacks)
- [Constants · The Julia Language](https://docs.julialang.org/en/v1/base/constants/#Base.LOAD_PATH)
- [Environment Variables · The Julia Language](https://docs.julialang.org/en/v1/manual/environment-variables/#JULIA_LOAD_PATH)

For example, starting jupyter with `JULIA_LOAD_PATH="@:@stdlib" jupyter-notebook` should do it.

---

<div class="post-metadata">

### Author: ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)
#### Post date: [June 30, 2021, 4:02pm UTC](https://discourse.julialang.org/t/how-to-run-activate-a-jupyter-notebook-in-a-specific-environment/63841/8 "2021-06-30T16:02:16Z")

</div>

> `push!(empty!(LOAD_PATH), "@")` should do it.

So, I tried to run this in my Jupyter Notebook, but the result seems to be that my load path is empty? I can’t run `using` on any package (even those I tried to add to this environment). Also, `Base.load_path()` returns `String[]`.

> For example, starting jupyter with `JULIA_LOAD_PATH="@:@stdlib" jupyter-notebook` should do it.

If I add this thing, would it make Jupyter default loading any environment with toml files in the folder of the notebook? Also, would I need to to start jupyter from that specific folder, or could I have one jupyter program open, with several notebooks open in various folders, in various projects, in various environments?

---

<div class="post-metadata">

### Author: ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)
#### Post date: [June 30, 2021, 4:13pm UTC](https://discourse.julialang.org/t/how-to-run-activate-a-jupyter-notebook-in-a-specific-environment/63841/9 "2021-06-30T16:13:23Z")

</div>

So I did a bit of testing. If I create a new notebook in that folder, and then run

```julia
using Pkg
Pkg,status()

```

to check my environment, then if I create the notebook in Julia 1.5.2 I am in the local environment. However, if I do the same in Julia 1.6.1, it doesn’t seem to work. I am not sure why, but if there would be some way to resume the Julia 1.5.2 behaviour, that would be great!

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [June 30, 2021, 4:28pm UTC](https://discourse.julialang.org/t/how-to-run-activate-a-jupyter-notebook-in-a-specific-environment/63841/10 "2021-06-30T16:28:29Z")

</div>

This is a known bug, I just ran into it and fixed it with advice from here: [https://github.com/JuliaLang/Pkg.jl/issues/2542](https://github.com/JuliaLang/Pkg.jl/issues/2542)

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [June 30, 2021, 4:41pm UTC](https://discourse.julialang.org/t/how-to-run-activate-a-jupyter-notebook-in-a-specific-environment/63841/11 "2021-06-30T16:41:59Z")

</div>

> [@Torkel](#):
>
> So, I tried to run this in my Jupyter Notebook, but the result seems to be that my load path is empty? I can’t run `using` on any package (even those I tried to add to this environment). Also, `Base.load_path()` returns `String[]` .

Run `Pkg.activate()` again after you do this maybe?

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [June 30, 2021, 4:55pm UTC](https://discourse.julialang.org/t/how-to-run-activate-a-jupyter-notebook-in-a-specific-environment/63841/12 "2021-06-30T16:55:20Z")

</div>

> [@pdeffebach](#):
>
> Run `Pkg.activate()` again after you do this maybe?

I think IJulia does that automatically.

> [@Torkel](#):
>
> If I add this thing, would it make Jupyter default loading any environment with toml files in the folder of the notebook?

That’s the default behavior without any extra config.

> [@Torkel](#):
>
> would I need to to start jupyter from that specific folder, or could I have one jupyter program open, with several notebooks open in various folders, in various projects, in various environments?

The second one: `JULIA_LOAD_PATH="@:@stdlib"` is passed to each Julia instance, started for each notebook. Each of these Julia instances will interpret `@` and `@stdlib` “locally”. Unless I’m mistaken of course 🙂

Note that the Julia default is equivalent to `JULIA_LOAD_PATH="@:@v#.#:@stdlib"`. This means that Julia will load packages from the local project (`@`), from the global user environment (`@v#.#`) and from the Julia installation (`@stdlib`). All we’re doing is removing the `@v#.#`.

---

<div class="post-metadata">

### Author: ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)
#### Post date: [June 30, 2021, 5:38pm UTC](https://discourse.julialang.org/t/how-to-run-activate-a-jupyter-notebook-in-a-specific-environment/63841/13 "2021-06-30T17:38:35Z")

</div>

Thanks for all the help everyone 🙂 I think I understand better what is going on now, and also learnt a little.
