Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Conda, Packages, and Jupyter Notebook

Authors
Affiliations
University of Oxford
University of Toronto / University of Cambridge

Overview

By this point, you have already:

  • ✅ Installed Python

  • ✅ Installed Visual Studio Code

  • ✅ Installed the Python extension for Visual Studio Code

  • ✅ Created and ran your first .py file

The next step is learning how to manage Python environments, install packages, and write code in Jupyter Notebooks (.ipynb), which are widely used in data science and neuroscience.


Why Do We Need Environments?

As your projects become larger, you’ll begin installing many additional pieces of software.

For example:

  • NumPy

  • Pandas

  • Matplotlib

  • Scikit-learn

  • Nilearn

These are called packages (sometimes called libraries).

A package is simply a collection of code written by other programmers that you can reuse instead of writing everything yourself.

For example:

  • NumPy provides fast numerical arrays and mathematical operations.

  • Pandas makes it easy to work with tables and spreadsheets.

  • Matplotlib creates figures and graphs.

  • Nilearn contains tools for analyzing brain imaging data.

Instead of reinventing the wheel, we simply install these packages and use them in our own programs.


Why Not Install Everything Everywhere?

Imagine installing every package you’ve ever needed into one giant Python installation.

Eventually:

  • different projects may require different package versions,

  • packages may conflict with one another,

  • updating one package could accidentally break another project.

To avoid this, we create environments.

An environment is an isolated Python workspace containing:

  • its own Python installation,

  • its own packages,

  • its own dependencies (the software required for packages to work).

Think of an environment as a separate toolbox for each project.

For example, you might eventually have three environments:

  • neuroimaging

  • education

  • quantfinance

Each can have different packages without affecting the others.

The original Python installation is often called the base environment. In general, it is good practice to leave the base environment as clean as possible and create a new environment for each project.


Installing Conda

Conda is a tool that creates and manages environments.

Follow the video below from 1:45 onwards to 10:10:

https://www.youtube.com/watch?v=qI3P7zMMsgY&t=105s

The video covers:

  • installing Conda,

  • what environments are,

  • why environments are useful,

  • creating environments,

  • removing environments.

Some terminology may be unfamiliar at first. That’s completely fine. The goal is simply to become familiar with the overall workflow.

The video goes very briefly over installing conda.

For Mac, follow this written guide to install conda: https://www.anaconda.com/docs/getting-started/anaconda/install/mac-gui-install

For Windows, follow this written guide to install conda: https://www.datacamp.com/tutorial/installing-anaconda-windows

Important (for Windows):

In Step 6 of the written guide, tick “Add Anaconda to my PATH environment variable”, even though the installer says this is not recommended. Tim explains why between 3:03 and 3:39 of the video.

Continue watching the video until approximately 10:10.

You can ignore the sections on:

  • Visual Studio Code

  • IntelliSense / Autocomplete

  • VS Code Extensions


Conda Cheat Sheet

The official Conda cheat sheet is a useful reference:

https://docs.conda.io/projects/conda/en/latest/user-guide/cheatsheet.html

Briefly, have a look. Don’t worry about memorizing every command. We’ll only use a few of them regularly.


Creating Your First Environment

Go to your Terminal or Powershell.

The command you’ll probably use most often is:

conda create --name ENVNAME python=3.11

Replace ENVNAME with a descriptive name.

For example:

conda create --name neuroimaging python=3.11

This creates a new environment called neuroimaging with Python 3.11 installed.

Why specify the Python version?

You may notice that the Conda cheat sheet simply shows:

conda create --name ENVNAME

This creates an empty environment.

Instead, we recommend specifying the Python version so Conda installs Python inside the environment immediately. This allows Visual Studio Code to recognize it as a Python environment.

If you’re unsure which Python version to choose, you can choose Python 3.11 since it is widely supported by many packages.


Activating an Environment

Before installing packages, you first need to activate your environment.

conda activate neuroimaging

If the command succeeds, you should see something similar to:

(neuroimaging)

at the beginning of your Terminal or PowerShell prompt.

This tells you that any packages you install will be installed inside the neuroimaging environment, rather than into your global (base) Python installation.


Viewing Installed Packages

To see which packages are currently installed, type:

conda list

You’ll see many packages already installed.

Don’t worry about understanding them yet.

Most are supporting software that Python requires to run.


Installing Packages

Now let’s install two of the most commonly used data science packages.

NumPy

NumPy provides fast arrays and numerical computations.

Official installation instructions:

https://numpy.org/install/

Most packages provide installation instructions in their documentation.

For NumPy, you can install it using either:

conda install numpy

or

pip install numpy

If the package provides Conda instructions, we recommend using Conda.

Otherwise, pip install PACKAGENAME usually works well.

Regardless of which method you use, always activate your environment first via your Terminal / Powershell.

Once you run conda install numpy, and when prompted:

Proceed ([y]/n)?

simply type:

y

and press Enter.

After installation, run:

conda list

again.

You should now see numpy in the list.


Pandas

Pandas is a package for working with tabular data (similar to spreadsheets).

Similar to NumPy, we can google their installation instructions. Once you do that, you will hopefully find this link:

https://pandas.pydata.org/docs/getting_started/install.html

You may see instructions such as:

conda install -c conda-forge pandas

instead of the usual:

conda install pandas

Both install Pandas using Conda.

The first explicitly downloads the package from the conda-forge channel, while the second uses your default Conda channels.

You can follow the instructions and type conda install -c conda-forge pandas. If you’re unsure which command to use, feel free to ask ChatGPT to explain the differences.


Using Jupyter Notebook

From the previous tutorial, we’ve learned how to write and run Python programs using a .py file. However, data scientists and neuroscientists may prefer using Jupyter Notebooks (.ipynb), which allow you to combine code, figures, and notes in a single document. In fact, we’ll be mostly using Jupyter Notebooks!

Follow the YouTube tutorial on the Visual Studio Code website:

https://code.visualstudio.com/docs/datascience/jupyter-notebooks

You can follow the tutorial as usual until she reaches Select Kernel → Python Environments (approximately 1:14–1:30).

At this point, do not create a new lightweight environment by typing:

python -m venv ./venv

Instead, you should see the Conda environment that you created earlier in this guide. It may look something like:

neuroimaging (Python 3.11.15) /opt/anaconda3/envs/neuroimaging/bin/python

(Your exact Python version and file path may be slightly different.)

Select this environment as your Jupyter kernel, then continue following the tutorial from approximately 1:53 onwards.

As you work through the tutorial, focus on learning how to:

  • create Code cells,

  • create Markdown cells,

  • run individual cells.

After watching the video, you may also find the written guide helpful. But don’t worry about remembering every feature of Jupyter Notebook. You’ll become more comfortable with it as you continue writing code.


Testing Your Installation

Create a new .ipynb notebook on VS Code and make sure the environment in which you installed numpy and pandas is activated by clicking the Select Kernel.

Create a new Code cell and type:

import numpy as np
import pandas as pd

The import statement tells Python that we’d like to use these packages in our program.

The shorter names:

np
pd

are standard abbreviations that you’ll see throughout the scientific Python community.

Run the cell.

If no error messages appear, congratulations!

Both NumPy and Pandas have been installed successfully.

If you receive an error, first check that you’ve selected the correct Jupyter kernel (e.g., your neuroimaging environment). Installing a package into one environment does not automatically make it available in another.