Skip to content

Instantly share code, notes, and snippets.

@dpaletti
Last active September 1, 2023 20:43
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpaletti/da7729c35ba7f9274c3635849608b7bc to your computer and use it in GitHub Desktop.
Save dpaletti/da7729c35ba7f9274c3635849608b7bc to your computer and use it in GitHub Desktop.
Run python poetry project on Colab

Python Poetry Project on Colab

Clone Github Repository

Move in "/content"

  import os
  os.chdir("/content")

Setup git email and username

 !git config --global user.email <"Your Email">
 !git config --global user.name <"Your Username">

Clone the project from git

 !git clone https://<token>@github.com/<Github Username>/<Repository Name>.git

Install Poetry and Project Dependencies

Install poetry from pip

!pip install poetry

Move in the project folder (created after cloning the repository from git)

import os
os.chdir("/content/<Repository Name>")

Configure poetry not to create virtual environments in the project folder

!poetry config virtualenvs.in-project false

Install all dependencies and eventually add extra dependencies

!poetry install
# !poetry run pip install <Extra Dependency Name>  

Run Project

Add poetry virtual environment to python path so that all installed dependencies can be found by the python interpreter

  import sys
  sys.path.append("/root/.cache/pypoetry/virtualenvs/<Virtual Environment Name>/lib/<Python Version>/site-packages")

Move in the project folder and run the project

  import os
  os.chdir("/content/<Repository Name>")
  !poetry run python <Path to main.py> <Arguments>
@sirykd
Copy link

sirykd commented Jan 31, 2023

Hi @elise-chin-adway,

I have encountered the same issue with poetry on Colab.
Running poetry install --no-ansi, as suggested in python-poetry/poetry#7184, worked for me.

@elise-chin-adway
Copy link

Hi @sirykd,

Thanks for your reply, it works!
And after all, my main issue was that pyproject.toml was not updated automatically, so I just decided to modify it by hand.
Here is the steps for using poetry in Colab, whether you create your own poetry project, or cloning a repo on Github.

https://github.com/elise-chin-adway/poetry-and-colab/blob/main/Using_python_poetry_in_Google_Colab.ipynb

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