Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bunsyy/62aaf946b970b362ad6ed68b2bf0baf9 to your computer and use it in GitHub Desktop.
Save bunsyy/62aaf946b970b362ad6ed68b2bf0baf9 to your computer and use it in GitHub Desktop.
How I use Poetry with Jupyter Notebook in VSCode

Note

  • You can copy any part that you want. Feel free!
  • You can skip Step 1 and 2 if you aleady installed Poetry.

Poetry Setup Guide

Step 1 - Install Poetry

python3 -m pip install poetry

Step 2 - Verify Poetry Installation

poetry --version

Step 3 - Initialize Poetry in your Jupyter Notebook project

poetry init

Step 4 - Install dependencies

poetry add tensorflow matplotlib ipykernel

Step 5 - Get Poetry Environment Info Path

poetry env info --path

Step 6 - Activate the Poetry Environment

source your-poetry-venv/bin/activate

Step 7: Add the Poetry Environment as a Jupyter Kernel

ipython kernel install --name "your-new-poetry-env-name" --user
# OR poetry run ipython kernel install --name "tessaract-ocr-python-tuto" --user
python3 -m pip install poetry
poetry --version
poetry init
poetry add tensorflow matplotlib ipykernel
poetry env info --path
source your-poetry-venv/bin/activate
ipython kernel install --name "your-new-poetry-env-name" --user
# or
# poetry run ipython kernel install --name "your-new-poetry-env-name" --user
@Ketan-K17
Copy link

I'm a bit of a beginner here... how do you set up the project after that last command? I can't find the environment i created in the kernels..

@bunsyy
Copy link
Author

bunsyy commented Nov 8, 2024

Hi @Ketan-K17, sorry for the late reply.

  • Can you ensure that you have run poetry install successfully?
  • After that you should verify that poetry env info --path gives you a proper virtual environment path like this:
result of poetry env info --path
  • Then, you should run this command (step 6) without error:
activate the bin

Here, you might get an error because /bin/activate does not exist.

  • You can start the kernel with the command in step 7:
activate the bin
  • Finally, you can refresh the kernel (see the button inside the red square):
activate the bin

@LYK-love
Copy link

LYK-love commented Feb 5, 2025

I think poetry is a tool that works upon conda (or pip), not equivalent to conda. Using poetry inside a conda env is easier than using a poetry env directly.

@JoaoKbral
Copy link

So, I'm not sure if this has ended or not but when you first run the project vscode will ask you to choose a kernel, select the one that poetry created, that's all
image
if it doesn't ask you can click in the place that shows the kernel version that is highligthed
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment