Poetry is a powerful dependency management and packaging tool for Python projects, offering isolated virtual environments and reproducible builds. Using Jupyter Notebook with Poetry allows you to work seamlessly within the same environment managed by Poetry, ensuring all your dependencies are consistent.

This guide will take you through the steps to get up and running with Jupyter Notebook using Poetry.


Step 1: Install Poetry Using pip

You can install Poetry using pip, the Python package installer. Run the following command:

pip install poetry

After installation, verify that Poetry is installed and accessible:

poetry --version

Step 2: Create a New Poetry Project

Create a new directory for your project and initialize it with Poetry:

mkdir my-jupyter-project
cd my-jupyter-project
poetry init --no-interaction

This generates a pyproject.toml file to manage your project’s dependencies.


Step 3: Add Jupyter and ipykernel as Dependencies

Add Jupyter Notebook as a development dependency:

poetry add --dev jupyter ipykernel

You can also add other libraries you plan to use (e.g., pandas, numpy):

poetry add pandas numpy

Step 4: Install the Jupyter Kernel for Your Poetry Environment

Make your Poetry virtual environment available as a kernel in Jupyter so you can select it when you launch notebooks:

poetry run python -m ipykernel install --user --name=my-jupyter-project

Replace my-jupyter-project with a meaningful name for your kernel.


Step 5: Launch Jupyter Notebook

Run Jupyter Notebook using Poetry to ensure you are using the correct virtual environment:

poetry run jupyter notebook

This command will start Jupyter Notebook in your browser. When you create or open a notebook, make sure to select the kernel named after your Poetry environment (my-jupyter-project in this example).


Step 6: Start Coding!

You now have a fully isolated environment managed by Poetry, using Jupyter Notebook for your interactive computing. All the dependencies installed via Poetry are ready to use.


Optional: Using Jupyter Lab

If you prefer Jupyter Lab, you can add and run it similarly:

poetry add --dev jupyterlab
poetry run jupyter lab

This method ensures your Jupyter notebooks are reproducible, isolated, and aligned with your Poetry-managed Python environment, improving project consistency and collaboration.

If you use VSCode, be sure to select the Poetry virtual environment interpreter and the corresponding Jupyter kernel to have a smoother development experience.

Enjoy coding with Poetry and Jupyter!