Updating version of python on Google Colab

Question:

I need to update the default python version in Google Colab to version 3.9 in order to install some packages via poetry. However, when I update python I seem to lose the ability to install anything at all. I’m updating python by following the accepted answer here, which works in so far as giving me the version of python I need:

#install python 3.9
!sudo apt-get update -y
!sudo apt-get install python3.9

#change alternatives
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2

#check python version
!python --version
# returns Python 3.9.13

However, when I then try to install poetry via !pip3 install poetry I get the following response:

Traceback (most recent call last):
  File "/usr/local/bin/pip3", line 5, in <module>
    from pip._internal.cli.main import main
ModuleNotFoundError: No module named 'pip'

Curiously, when I try !pip3 install poetry before updating python it works. However, if I install poetry first, then update python, when I try to install the variopus packages I need via !poetry install I get a similar traceback error:

Traceback (most recent call last):
  File "/usr/local/bin/poetry", line 5, in <module>
    from poetry.console import main
ModuleNotFoundError: No module named 'poetry'

Long-story-short, I can’t work out how I can get the version 3.9 of python in Colab AND poetry to work.

Asked By: DC_Liv

||

Answers:

Yes you need to install pip with !sudo apt-get install python-pip

Then you might need the distutils lib !sudo apt install python3.9-distutils

Answered By: T0w0T