Environments - understanding basic use?

Advice needed – I finally try to learn the basics of Environments (documentation I’ve found seems to cover more complex cases than mine…)

Below, I describe my Case, and what we have done so far. I’m interested in feedback on whether this is the (simplest) way to do it.

Case:

  • I am working on some code in a Jupyter notebook in VSCode. I use the latest versions of Julia and all packages.
  • I need to compare my results to that of a student who studied the same problem, but used an older version of Julia and older versions of packages. Her Jupyter notebook does not work with the latest versions of Julia and packages.
  • Because the two Jupyter notebooks deal with the same project, I want to put them in the same directory.

Here is what we have done

  1. Student added the following code at the top of her notebook:
using Pkg
Pkg.activate(".")

followed by importing the packages she used, etc.

  1. This generated a Project.toml file and a Manifest.toml file in her directory.

  2. She sent an e-mail to me with (i) her Jupyter notebook, (ii) the Project.toml file, and (iii) the Manifest.toml file attached.

  3. I saved all three of these files in the same directory as my similar Jupyter notebook.

  4. I opened her Jupyter notebook, and ran it. Error message! Probably because I did not have on my computer the version of Julia and the package versions she used when she generated the *.toml files.

  • So I added a Pkg.instantiate() command in her notebook just after the Pkg.activate(".") command.
  • Now, Julia and packages were downloaded and compiled. This took some 10-15 minutes.
    -When done, her notebook ran perfectly on my computer.
    -I notice that in the terminal window of VSCode, if I enter the package environment (]), the prompt still shows the latest version of Julia ((@v1.10) pkg> ), and not the version the student used (which was v1.9.3) or the name of the directory.
  1. I then switched back to my own notebook, and ran it. Big shock: Julia started to recompile all packages, and eventually crashed.
  • I assume this is because I did not close VSCode and restart, so Julia is still in the environment of the student.
  • By closing and restarting VSCode, my code seemed to work again – at least if I renamed the *.toml files in the directory of my Jupyter notebooks.
  1. I have now inserted command Pkg.activate() at the top of my own Jupyter notebook, before I import packages.
  • This seems to “deactivate” the invoked environments and use the global environment.
  • Now I can switch between the two notebooks, and both run as expected.

Final comment:

  • Yes, we should probably use GitHub, and not exchange files by e-mail…
  • It still puzzles me that the package prompt in the terminal window seems to show my current (global) Julia version whether I run in the global environment or the local environment – this seems to be contrary to documentation. But maybe this is a VSCode thing.

It is not clear me that the Jupyter notebook and the terminal would be using the same Julia session. In fact, I’m almost certain that they are distinct Julia sessions.

For each Julia session, you need to activate the same environment as the original notebook. The path . refers to the current working directory of the Jupyter notebook. You can obtain the full path to the current working directory via the command pwd.

Once you have the full path to the environment, you can activate it from anywhere via

Pkg.activate("/full/path/to/env")

In VSCode, with there is also a way to change the Julia environment for your VSCode workspace. On the bottom left of the screen in the status bar there is an indicator of the current Julia environment. If you click on that you can select another environment.

See the environment selection UI here:
https://www.julia-vscode.org/docs/stable/userguide/env/

3 Likes