How to properly create, activate, and resume, an environment in VSCode

I am trying to create a new Julia environment in VSCode, and run some stuff from it. However, I have a feeling that I might not have really grasped how to do this properly. It would be good if someone could have a look at my workflow.

  • First I create a new folder on my desktop; “My_New_Project”.
  • Next, I open VSCode. It opens with a terminal open. Checking with ls there, it seems to be in my home directory. I write cd Desktop/My_New_Project to change the folder to where I want my new environment.
  • Next, I open a Julia REPL. Checking with pwd() it seems to be in my home folder. I run ; cd Desktop/My_New_Project to move Julia to my desired project folder.
  • I then run ] activate . to activate the folder as a new Julia project.
  • I run ] add DifferentialEquations, ModelingToolkit, Catalyst, Plots to add the packages I require for my project.
  • I check with ] status that indeed only these packages are active in my environment.
  • I run ] dev ModelingToolkit, because I require the master of the ModelingToolkit package (I have the latest version pulled to my “.julia/dev” folder.
  • I now close VSCode (reasons unknown, but sometimes you require to close it, and I initially had some trouble with resuming stuff).
  • I now repeat the steps 2-4. Here, running ] activate . again allows me to resume the previous environment? Running ] status seems to confirm that.

Is this the proper/correct way of doing things? Is there some smoother way of doing things?

Also, a follow-up. Part of the project I would like to do in jupyter notebooks. Here I open a jupyter notebook, navigate to “Desktop/My_New_Project”, and create a new notebook there. Checking with

using Pkg
Pkg.status()

this notebook seems to be in this new environment. It seems jupyter notebooks automatically detect it, but VSCode requires you to specifically activate each time?

I believe there is a project setting (workspace setting? not sure about the terminology) that remembers the active project in VS code.

From my experience, the environment that VSCode’s Julia extension (I’ll just call this VSCode) knows about is independent of the environment that the Julia REPL knows about. The steps you describe are you establishing and maintaining an environment with the Julia REPL (e.g. manipulating the Project.toml and Manifest.toml files for your directory). What you write looks good to me for selecting the environment and adding packages from the REPL.

If you want VSCode to use that environment for its Intellisense, then you must tell VSCode that. If you have a Julia file loaded in VSCode, you’ll see a “Julia env: …” thing in the status bar (bottom of the window). That’s the environment that VSCode is using. I’ve noticed that VSCode can figure out to use the environment for a project - but not always (I haven’t looked at this carefully enough to learn when it does and doesn’t). But you can always tell it … just click on that “Julia env:…” thing on the status bar and it will ask you to select the directory with the environment you want. It’ll then run the language server indexing and will set up Intellisense. This works well. Note that if you change the environment (e.g. add packages), VSCode will not notice the new packages. Follow the procedure here and select the directory again. That will trigger a re-indexing by the language server and then Intellisense will work for the new packages you added.

This may seem a little clunky - but I’ve gotten used to it and it works well. Keeping your eye on “Julia env:…” in the status bar and clicking on it when necessary is the key to making VSCode understand the environment you want. Hope this helps. – Adam

1 Like

… and I forgot to mention that VSCode will remember the environment you selected for that project.

My standard workflow looks like this (I’m on Windows):

Open PowerShell then:

  • cd wherever/I/want/my/project

  • mkdir ProjectName

  • cd ProjectName

  • code .

Now, VS Code will be open in the directory that I just created. I open a Julia REPL from the VS Code menu and then

  • ] to enter the package manager

  • activate .

  • add Whatever Packages Are Needed

After adding packages I have a Project.toml so I click in the lower left:

image

and select the environment ProjectName. Now, every time I want to go back to this project and continue working on it I simply cd into it via PowerShell and type the code . command and it will open in VSCode and the correct environment will be automatically loaded since there is a Project.toml.

1 Like