Google colab python version

How can I update Google Colab’s Python version?

The current default version of Python running on Google Colab is 3.7, but I need 3.9 for my notebooks to work. How can I update Google Colab’s Python version to 3.9 (or greater)?

7 Answers 7

In Google Colab you have a Debian-based Linux, and you can do whatever you can on a Debian Linux. Upgrading Python is as easy as upgrading it on your own Linux system.

Detect the current python version in Colab:

!python --version #Python 3.8.16 

Let’s first install and upgrade to Python 3.9:

#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.8 1 !sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2 #check python version !python --version #3.9.16 

As mentioned in the comments, the above commands just add a new python version to your google colab and update the default python for commandline usage. But your runtime packages such as sys are still running on the previous python version. The following commands need to be executed as well, to update the sys version.

# install pip for new python !sudo apt-get install python3.9-distutils !wget https://bootstrap.pypa.io/get-pip.py !python get-pip.py # credit of these last two commands blongs to @Erik # install colab's dependencies !python -m pip install ipython ipython_genutils ipykernel jupyter_console prompt_toolkit httplib2 astor # link to the old google package !ln -s /usr/local/lib/python3.8/dist-packages/google \ /usr/local/lib/python3.9/dist-packages/google 

Now you can restart runtime and check the sys version. Note that in the new python version you have to install every packages, such as pandas, tensorflow, etc. from scratch.

Читайте также:  Update image in html

Also, note that you can see a list of installed Python versions and switch between them at any time with this command: (If nothing changed after installation, use this command to select python version manually)

!sudo update-alternatives --config python3 #after running, enter the row number of the python version you want. 

Unfortunately this is not working. It install python 3.9 but the Colab Kernel is still running on 3.7.

@Conchylicultor If you’re sure you have already installed Python 3.9, then you can change the default python in use by this command !sudo update-alternatives —config python3 or !sudo update-alternatives —config python manually.

!python —version show me 3.9.12 , but import sys ; sys.version run on the colab itself still show 3.7: imgur.com/a/jfkc9km

@Conchylicultor You mean runtime python. My answer only adds a new python to your runtime for the command line usages. But of course, all developing packages are already running on the default python version. If you want to port all your packages to the new python version you need to do some further steps. Because, with the above steps you have just updated your python, and it is a raw python without any packages even pip.

@Conchylicultor For example You need to do !sudo apt-get install python3.9-distutils && wget https://bootstrap.pypa.io/get-pip.py && python get-pip.py to install pip for new python. And then I think you need to install fresh ipykernel , and even google colab, and I don’t know even it is compatible with new python versions or not.

It’s also possible to update the kernel without going through ngrok or conda with some creative package installation.

Raha’s answer suggesting making a link between the default google package and the newly installed Python version is the trick that makes this work because, at least with Python 3.9, the version of pandas (0.24.0) that the google package requires fails to build.

Here’s the code I used to install and switch my Colab kernel to Python 3.9:

#install python 3.9 and dev utils #you may not need all the dev libraries, but I haven't tested which aren't necessary. !sudo apt-get update -y !sudo apt-get install python3.9 python3.9-dev python3.9-distutils libpython3.9-dev #change alternatives !sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1 !sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2 #Check that it points at the right location !python3 --version # install pip !curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py !python3 get-pip.py --force-reinstall #install colab's dependencies !python3 -m pip install ipython ipython_genutils ipykernel jupyter_console prompt_toolkit httplib2 astor # link to the old google package !ln -s /usr/local/lib/python3.8/dist-packages/google \ /usr/local/lib/python3.9/dist-packages/google # There has got to be a better way to do this. but there's a bad import in some of the colab files # IPython no longer exposes traitlets like this, it's a separate package now !sed -i "s/from IPython.utils import traitlets as _traitlets/import traitlets as _traitlets/" /usr/local/lib/python3.9/dist-packages/google/colab/*.py !sed -i "s/from IPython.utils import traitlets/import traitlets/" /usr/local/lib/python3.9/dist-packages/google/colab/*.py 

If Google updates from Python 3.8, you’ll have to change the path to the default package.

Then go the Runtime menu and select Restart runtime . It should reconnect and choose the updated version of Python as the default kernel. You can check that it worked with:

#check python version import sys print(sys.version) !python3 --version !python --version 

Источник

As of 2021, which python version is Google Colab using? [duplicate]

The only way to use Python 3.8 would be to connect to a local runtime. But, that’ll kill the purpose of Colab.

I make a library to help with this (and other issues)

!pip install kora -q import kora.install.py38 

Wait a bit, then click the link.

According to their FAQ they are actively phasing out Python 2 in favor of Python 3. There are some breaking changes between early versions of Python 3.x and later ones, but these largely occurred prior to most of the world making the jump to Python 3. You are probably safe using Python 3.8 for this reason alone, but given the way workbooks run you’re probably safe from multiple angles.

The easiest way to build confidence Python 3.8 is compatible with what you want to do is to try it out — or at least a snippet. Import a library, instantiate an object, do some trivial manipulation of it and check that it works. If no errors arise when you run it you’re probably fine for most things — and where you might not be the error messages will be your guide.

So, I dug deeper and the FAQ links to Python 3.9 — so it should be fine for most cases. It is the case, however, that Google has only declared 3.6 support (so far as I can tell). That said, I’d be surprised if you ran into any real incompatibilities. If their runtime is on 3.6 you won’t be able to use walrus operators or position-only params, along with a handful of other features. Also, dictionaries will work differently under the hood (which may affect your programs behavior if you rely on that). That said, in truth these are edge-ish cases; is there a feature in particular you are worried about?

Источник

How to Change Python Version in Google Colab : 3 Steps Only

Convert dict to dataFrame python

Most of the coders or developers use Google Colab for executing their codes very fast as it provides you access to faster GPUs like the T4 and P100 if resources are available at a very cheap rate. But sometimes your code requires a specific version of the package to be installed. If you want to change the version of Python in Google colab then you have come to the right place. In this tutorial, you will learn how to change the Python version in Google Colab.

What is Google Colab ?

Google Colab is just like Jupyter Notebook that is managed by Google. It allows you to write python programming and execute it online through web browser faster. Most of the coders use it for machine learning, data analysis and education. Scholars use it for research purposes.

Problem Statement

You have installed the specific Python version in Google Colab let’s say it is 3.10. Now you want to change the version of Python to 3.7 then how you can do so? You will know the methods to do so in the next sections.

How to change the Python version in google colab

Before changing the version of the python in Google Colab let’s check the current version of the python installed in the collab. Use the below lines of code to check that

Steps to change the Python version of the Google Colab

Step 1: Update the alternatives for the python

The first step is to define the alternatives for the Python you want to install the python version. It will install the specific Python version in the colab. Here I will install Python version 3.7

Type the below command in your cell of the Google colab notebook to change the version.

Step 2: Configure the python

The second step is to configure the python version. Use the below command to do so.

It will ask the to type the selection number. Choose the number corresponding to your version. It will change the version of python. In this example, it is 3.7 so type 3 and enter.

Choose the version for the python

Step 3: Check the result

Now after all the above steps check the version of python to verify.

At last, you have to install python to permanently install the specific version to the google colab.

Conclusion

Sometimes a specific version of python is required to work for the python modules. If there is a requirement to change the version in the Google Colab then the above steps will change the version of python.

I hope you have liked this tutorial. If you have any queries then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

We respect your privacy and take protecting it seriously

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Источник

how to use python 3.6 in google Colab

I want to work with python 3.6 due to some file compatibility issue, can anyone help me out, where I can use python 3.6 in google Colab, apart from that I want to use tensorflow 2.0 and opencv 4.0.0.21, which I have already install in Colab am only stuck with python 3.6. any help would be appreciated

2 Answers 2

# first install python 3.6 !sudo apt-get update -y !sudo apt-get install python3.6 # change alternatives !sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1 # select python version !sudo update-alternatives --config python3 # check python version !python --version # install pip for new python !sudo apt-get install python3.6-distutils !wget https://bootstrap.pypa.io/get-pip.py !python get-pip.py # upgrade pip !sudo apt install python3-pip !python -m pip install --upgrade pip 

Please clarify the link for the pip3.6 as follows: https://bootstrap.pypa.io/pip/3.6/get-pip.py . The one mentioned in answer is already for 3.7 or later versions

You can use the command below and then select the alternative version to change the python version.

!sudo update-alternatives --config python3 
There are 2 choices for the alternative python3 (providing /usr/bin/python3). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/bin/python3.7 2 auto mode 1 /usr/bin/python3.6 1 manual mode 2 /usr/bin/python3.7 2 manual mode Press to keep the current choice[*], or type selection number: 1 update-alternatives: using /usr/bin/python3.6 to provide /usr/bin/python3 (python3) in manual mode 

To check the python version use the following code.

Источник

Оцените статью