How to add Jupyter to your Poetry project

So, you’re building a python app using poetry and you want to use a Jupyter Notebook in the same environment. It can be a pain but don’t worry, I’ve got your back.

In this post, I’m going to show you how to add a Jupyter kernel to your poetry environment. That way, you can avoid all the version conflicts and import issues that slow your progress to a halt.

How to add a Jupyter kernel with Poetry

If you’re using poetry for package management, you need to run this in your terminal:

poetry run python -m ipykernel install --name kernel-name

So, let’s get into what each of these pieces is.

poetry run python

This 👆 command will produce a python virtual environment. Note: Make sure you’re in the same directory as your pyproject.toml file or it won’t work. After running the command your terminal should look something like this:

I’m assuming that date is from when I installed Python 3.8.5? Weird.

Ok, so poetry run python activates a virtual environment. Then the -m flag runs a library as a script, as you can see in this screenshot from my terminal after running python --help:

The official Python docs read as follows:

The argument is a module name, you must not give a file extension (.py). The module-name should be a valid Python module name, but the implementation may not always enforce this (e.g. it may allow you to use a name that includes a hyphen).

Package names are also permitted. When a package name is supplied instead of a normal module, the interpreter will execute .main as the main module. This behavior is deliberately similar to the handling of directories and zip files that are passed to the interpreter as the script argument.

In plain English, using the -m flag tells your computer to run the module name, in this case, ipykernel.

Finally, this argument in the command names your kernel:

--name kernel-name

In closing…

I hope this was helpful! If you run into issues or have any questions, drop a comment or shoot me an email and I’ll get you squared away.

3 comments
Leave a Reply

Your email address will not be published.