简体   繁体   中英

Is it possible to use Python 3.10 in Google Colab?

I would like to use the Structural Pattern Matching feature from Python 3.10 in Google Colab so using the commands

!sudo apt-get install python3.10
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1
!sudo update-alternatives --set python3 /usr/bin/python3.10

I was able to make !python --version output 3.10.0, but the print(sys.version) still outputs 3.7.12 in code cells and so the match cases statement raises SyntaxError

number = 1

match number:
    case 0:
        print("Error")
    case _:
        print(number)

Is there any way to make this work?

You can try these commands:

!update-alternatives --install /usr/bin/python python /usr/bin/python3.10

then

!update-alternatives --list python

This must display your downloaded Python version.

After that,

!sudo update-alternatives --config python
## !Set python3.10 as default.

Finally,

!sudo update-alternatives --set python /usr/bin/python3.10

then check your default Python version on colab.

!python3 --version

Try the following commands,

!sudo apt-get update -y
!sudo apt-get install python3.10
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 2

its working:

def http_status(status):
    match status:
        case 400:
            return "Bad request"
        case 401:
            return "Unauthorized"
        case 403:
            return "Forbidden"
        case 404:
            return "Not found"

在此处输入图像描述

I just Update my python Version then write a code as following:

def http_status(status):
    match status:
        case 400:
            return "Bad request"
        case 401 | 403:
            return "Authentication error"
        case 404:
            return "Not found"
        case _:
            return "Other error"

it works fine now在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM