Dr Seán Carroll

by Dr Seán Carroll

Lesson

Coding Environment Setup

9. Welcome to your new home - VS Code

In this lesson, our aim is to get comfortable with Visual Studio Code. I’ll show you around, we’ll set up a new environment and create our first Jupyter Notebook. Let’s start by launching Visual Studio Code.

Once launched, VS Code has three main areas:

  • The tool ribbon on the far left has a collection of tools.
  • An explorer window to the right of this. The contents of this window change based on which tool is selected from the tool palette. By default, it’s set to be the file explorer.
  • The main area to the right of the explorer window is the main work area where we edit files.
The Visual Studio Code interface showing tool ribbon, explorer window and main work area. | academy.digilab.co.uk

Figure 1. Visual Studio Code interface showing tool ribbon, explorer window and main work area.

Basic Orientation


Let’s kick things off by opening a folder from the file explorer. We can open the folder named Test, created previously on the Desktop. We’re asked if we trust the authors of the files in this folder, we can select yes, obviously. The file explorer on the left now reflects the contents of the folder - which is empty. By hovering over the file explorer (Fig. 2), we reveal a menu that allows us to,

  • create a new file in the folder,
  • create a new subfolder
  • refresh the explorer view
  • collapse all folders in the explorer.

We'll make use of these tools soon, first we’ll briefly cover the other tools in the left-side tool ribbon. We have:

  • A search tool
  • A source control tool,
  • A debugging tool,
  • An extensions manager.
The Visual Studio Code interface showing tool ribbon and explorer window tool bar. | academy.digilab.co.uk

Figure 2. Visual Studio Code interface showing tool ribbon and explorer window tool bar.

We’ll leave the search, source control and debug tools for now and quickly take a look at the extension manager - click it to change the file explorer window to an extension manager window. Now we’re presented with a list of installed and recommended extensions.

One extension you will want to install is the Jupyter extension, Fig. 3. You can search for and install this extension directly from the extensions manager window.

The Jupyter VS Code extension. | academy.digilab.co.uk

Figure 3. Jupyter VS Code extension.

Creating a new Jupyter Notebook


Let’s create a new Jupyter Notebook by clicking the New File button to the right of the folder name at the top of the file explorer window. We can name the file anything we like, e.g. my_notebook.ipynb. By giving the file the extension .ipynb, we indicate that it’s a Jupyter Notebook file we want to create.

You will see that the main window has changed - it’s now showing us the contents of our Jupyter Notebook. We’ll take a look at the details of working with Jupyter Notebooks in the next lesson. First, we have a couple of extra details to cover.

Kernels and Environments


Before we run any Python code in our Jupyter Notebook, we need to select a kernel. A kernel is a process responsible for executing the code in the notebook cells and returning the results. The kernel interprets the code, runs it, and communicates the output back to the notebook interface. So when we create a new Jupyter Notebook we need to select a kernel to handle the code execution. We can do this by clicking on the dropdown in the top right corner of the screen that says Select Kernel, Fig. 4.

Jupyter Notebook in VS Code showing Kernel selection dropdown | academy.digilab.co.uk

Figure 4. Jupyter Notebook in VS Code showing Kernel selection dropdown.

When you select the dropdown, you should see a default Python kernel associated with the Anaconda Distribution we installed earlier. This is the default kernel corresponding to the Python interpreter and packages installed in the base Anaconda environment. In my case the default base kernel uses Python 3.10.9.

We can select this Kernel and happily start coding. However, recall earlier, we said that we should be creating a new environment for each project we work on. As a reminder, this is so we can keep project dependencies from interacting (e.g. if different projects need to use different versions of the same library).

We can create a new environment, as discussed in the last lesson. However, when you create a new Conda environment, it is not automatically registered as a kernel with Jupyter Notebooks, and it will not provide the intended level of code isolation. This is because the default kernel (executing the code in our notebook) is associated with the base Anaconda environment.

So if we want to use a new isolated Python environment for our Jupyter Notebook project, we must create and register a new kernel associated with a specific Conda environment. Once registered, we can switch between kernels in the VS Code interface to use the desired Python version and isolated instances of the packages for our project.

Registering a new environment as a kernel


Let’s start by opening a terminal within VS Code. Do this by navigating to the terminal menu and selecting new terminal. This is the same as the external terminal we saw previously; it’s just more convenient to use a version of it that’s integrated into VS Code, Fig 5.

VS Code with integrated terminal | academy.digilab.co.uk

Figure 5. VS Code with integrated terminal.

We can create a new environment with the latest version of Python, called my_env and activate it as shown previously,

conda create --name my_env python=3
conda activate my_env

We can execute the command python -V and confirm that Python 3.11.3 was installed, the latest version at the time of recording. You should be fine with any Python 3 version.

Next, we need to install the ipykernel package (from the anaconda channel) in the Conda environment. We do this as follows:

conda install -c anaconda ipykernel

The final step is to register this conda environment as a Python kernel so that we can select it as the kernel to use when executing the Python code in our Jupyter Notebook. We do this with the following command template:

python -m ipykernel install --user --name=my_kernel_name

So, in our case that’s

python -m ipykernel install --user --name=my_env

Now we can select our new kernel from the dropdown, and our Jupyter Notebook will reference the Python version and packages that have been installed in that environment. Note that we don’t need to manually activate the environment in order to use the kernel, but we will if we want to import new packages.

Deleting environments and kernels


You will quickly find that you accumulate quite a lot of Python kernels if you’re diligently isolating your dependencies from different projects. So, you should also know how to delete kernels (and environments) when you’re done with them.

We’ve already discussed deleting environments, simply execute,

conda env remove --name my_env_name

However, removing a Conda environment does not automatically delete the associated kernel. These are separate entities, and you need to delete them individually.

When you remove a Conda environment, it deletes the environment's Python interpreter, installed packages, and related files. However, the Jupyter kernel registration, which is a configuration that points to a specific Python environment, remains registered.

To delete the associated Jupyter kernel:

  1. Locate the kernelspec folder for the kernel you want to delete. You can see the file path in the kernel selection dropdown.
  2. Delete the entire kernel folder using the command line or a FInder/File Explorer.
  3. Restart VS Code.
  4. Open a Jupyter Notebook and check the kernel selection dropdown. The deleted kernel should no longer appear in the list of available kernels.

If you remove an environment, remember to delete the Jupyter kernel right away; otherwise, the kernel will still appear in the list of available kernels, and attempting to use it may result in errors since the associated Python environment no longer exists.

Similarly, if you delete the kernel first, this only removes its registration with Jupyter Notebooks, it does not delete the associated Conda environment or any installed packages.

We’ve covered quite a lot of ground here and still not written any Python code, so let’s wrap up with a quick summary and get to writing some code in the next lesson.

  1. A kernel is responsible for executing code in Jupyter Notebook cells and returning the results.
  2. A default kernel is associated with the base Anaconda environment.
  3. Creating isolated Conda environments for each project is a good practice for dependency management.
  4. To achieve proper code isolation, it's necessary to register a new kernel associated with a specific Conda environment.
  5. Once registered, you can switch between kernels in the VS Code interface to use the desired Python version and isolated instances of the packages for your project.