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.


Overview

You’d like to learn to run Python in a Jupyter Notebook, and you want to get up and running quickly. This chapter is for you!

Here we will show you how to install and launch a JupyterLab session on your computer, create and edit your first notebook with Python code, then save your work and exit.

After this quickstart guide, we’ll give more details in Installing and Managing Python with Conda and our entire section on Getting Started with Jupyter.

Prerequisites

ConceptsImportanceNotes
Choosing a Python PlatformHelpful

Installing Python with Jupyter: quickstart

As mentioned in the Pythia Foundations Overview, Jupyter is a set of tools for interactive computing. Instead of writing Python (.py) files and running them in the terminal, we will now write code in a Jupyter Notebook using the JupyterLab interface. To run a Jupyter session, you will need to install some necessary packages into your conda environment.

Install Miniforge by following the instructions for your machine. If you already did this while following through Python in the Terminal, you are all set.

Next, create a conda environment with JupyterLab installed. In the terminal, type:

conda create --name pythia_foundations_env jupyterlab

Test that you have installed everything correctly by first activating your environment and then launching a JupyterLab session:

conda activate pythia_foundations_env
jupyter lab

A new window should open automatically in your default browser. You can change the browser when launching from the terminal with (for example):

jupyter lab --browser google-chrome

Running Python in Jupyter

  1. With your conda environment activated and Jupyter session launched (see above), create a directory to store our work. Let’s call it pythia_foundations.

    Jupyter GUI

    You can do this in the GUI left sidebar by clicking the new-folder icon. If you prefer to use the command line, you can access a terminal by clicking the icon under the “Other” heading in the Launcher.

  2. Create a new mysci.ipynb file within the pythia_foundations folder:

    Do this in the GUI on the left sidebar by clicking the “+” icon.

    This will open a new launcher window where you can select a Python kernel under the “Notebooks” heading for your project. You should see “Python 3” as in the screenshot above. Depending on the details of your system, you might see some additional buttons with different kernels.

    Selecting a kernel will open a Jupyter Notebook instance and add an untitled file to the left sidebar navigator, which you can then rename to mysci.ipynb.

    Select “Python 3” to use the Python version you just installed in the pythia_foundations_env conda environment.

  3. Change the first notebook cell to include the classic first command: printing, “Hello, world!”.

    print("Hello, world!")
  4. Run your cell with Shift+Enter and see that the results are printed below the cell.

    Jupyter - Hello World

Congratulations! You have just set up your first Python environment and run your first Python code in a Jupyter notebook.

Saving your notebook and exiting

When you are done with your work, it is time to save and exit.

To save your file, you can click the disc icon in the upper left Jupyter toolbar or use keyboard shortcuts.

Jupyter allows you to close the browser tab without shutting down the server. When you’re done working on your notebook, it’s important to click the “Shutdown” button on the dashboard to free up memory, especially on a shared system.

Then you can quit Jupyter by:

Alternatively you can simultaneously shutdown and exit the Jupyter session by typing Ctrl+C in the terminal and confirming that you do want to “shutdown this notebook server.”


Summary

Jupyter Notebooks are a free, open-source, interactive tool running inside a web browser that allows you to run Python code in “cells.” To run a Jupyter session you will need to install jupyterlab into your conda environment. Jupyter sessions need to be shutdown, not just exited.

What’s next?

Additional resources