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

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?

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

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?).

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().


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)

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

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

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

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?

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

using Pkg

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!

This is a known bug, I just ran into it and fixed it with advice from here: after upgrade to 1.6.1: lost Pkg prompt on Pkg.update() and Pkg.add("...") · Issue #2542 · JuliaLang/Pkg.jl · GitHub


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

I think IJulia does that automatically.

That’s the default behavior without any extra config.

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 :slight_smile:

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#.#.

1 Like

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

1 Like