A Beginner's Guide to Using Jupyter Notebooks: from Colab to VS Code

 

This guide covers three ways to use Jupyter Notebooks. Level 0 uses Google Colab, which runs in your browser with no installation needed. For local work, Level 1 uses Anaconda, an easy, all-in-one installation. Level 2 uses Miniconda and VS Code for a more professional and customizable setup, which is highly recommended for long-term project work.

 

1. Google Colab: Your Instant, Cloud-Based Notebook

 

Google Colaboratory, or Colab, is a free, cloud-hosted Jupyter Notebook environment that runs entirely in your web browser.1 It's an excellent starting point, especially for students and beginners, because it requires zero installation or setup on your personal computer.

Key Advantages:

Getting Started with Colab:

  1. Go to colab.google.com.

  2. Click "New Notebook" to create a new file.

  3. You'll see a "cell." Type Python code into it, like print("Hello from Colab!").

  4. Press Shift + Enter to run the code. The output will appear directly below the cell.

 

2. Conda Environments: Organizing Your Projects

 

Conda is a package and environment manager.6 While it's the tool that powers Anaconda and Miniconda, its most critical feature for any developer is its ability to create

virtual environments.

A virtual environment is an isolated, self-contained directory that holds a specific collection of packages and a specific Python version for a single project.7 Using environments is crucial for avoiding conflicts between projects that may require different versions of the same library.

The Golden Rule:** Don't Work in Your (base) Environment**

When you install Anaconda or Miniconda, it creates a default environment called base. You will see (base) at the start of your terminal prompt. It is a strong best practice to avoid installing project-specific packages directly into the base environment.

Think of the base environment as the superintendent of an apartment building—its job is to manage the building itself (conda and its core components). Each of your projects should live in its own apartment (a separate conda environment) with its own set of tools and furniture (packages). This keeps your base clean and ensures your projects are organized, independent, and reproducible.

Essential Conda Commands:

 

3. VS Code: Your Professional Development Hub

 

Visual Studio Code (VS Code) is a free, powerful, and highly popular code editor. It can be integrated with your conda environments to create a seamless workflow for writing Python scripts and working with Jupyter Notebooks locally.

Setting Up VS Code with Conda:

  1. Install the Python Extension: Open VS Code, go to the Extensions view (the icon with four squares), search for "Python," and install the official extension from Microsoft.

  2. Open Your Project Folder: Use File > Open Folder.. to open the directory containing your project files.

  3. Select Your Conda Environment: Open the Command Palette with Ctrl+Shift+P (Windows/Linux) or Cmd+Shift+P (macOS). Type Python: Select Interpreter and choose the interpreter associated with the conda environment you created (e.g., Python 3.11 ('my-project-env': conda)). This tells VS Code to use the Python and packages from that specific environment for your project.

Using Jupyter Notebooks in VS Code:

VS Code has native support for Jupyter Notebooks (.ipynb files), providing a rich user interface for running cells, viewing outputs, and debugging.

  1. Create or Open a Notebook: Create a new file with a .ipynb extension or open an existing one.

  2. Install the Jupyter Kernel Bridge: For VS Code to "see" your conda environment as a runnable Jupyter kernel, you must install one more package inside that environment. This is a critical and often-missed step.

    • Open your terminal.

    • Activate your environment: conda activate my-project-env

    • Install the ipykernel package: conda install ipykernel

      This package acts as a bridge, allowing VS Code's Jupyter interface to communicate with your conda environment.

  3. Select the Kernel: In the top-right corner of the notebook in VS Code, click "Select Kernel." You should now see your conda environment (my-project-env) in the list. Select it.

  4. Run Code: You can now write code in cells and execute them with Shift + Enter or the "Run" button, just like in Colab. The code will be executed by the Python interpreter and packages from your selected conda environment.

  5. Explore Your Data: VS Code offers powerful tools like a Variable Explorer to inspect dataframes and other objects in memory, and a full-featured debugger for stepping through your code cell by cell.