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>
@elise-chin-adway
Copy link

elise-chin-adway commented Jan 20, 2023

Hi, thank you for this tutorial! It is well explained 😄

I tried to reproduce it but unfortunately, I encountered some issues.

1. poetry install does not work

I have a github repo with poetry.lock and pyproject.toml, I cloned it using Colab, moved in the project folder as you specified, but then when I run !poetry install, no packages are installed in the virtual environment, and not even in Colab environment... The output is the following:

Installing dependencies from lock file

Package operations: 35 installs, 0 updates, 0 removals

I can't find why it is not working 😣

2. poetry add <package>

According to poetry's documentation, to install a new package, we need to run poetry add. I ran this command, but again no packages are installed. Moreover, the poetry.lock file is indeed updated, but not the pyproject.toml. This happens only on Colab, I tried on my local computer, and the command indeed automatically update the pyproject.toml file as well.

3. poetry run pip install <package>

So I followed the above command, that you indicated in this tutorial. The packages are installed in the virtual environment, but pyproject.toml is not updated.
Also, what is the difference between poetry run pip install <package> and poetry add <package>?

Thank you in advance for your help 😄

@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