是否可以在Google Colab中使用Python 3.10?

raogr8fs  于 2022-10-30  发布在  Python
关注(0)|答案(2)|浏览(228)

我想在Google Colab中使用Python 3.10的结构模式匹配功能,因此使用以下命令

!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

我可以使!python --version输出3.10.0,但print(sys.version)在代码单元格中仍输出3.7.12,因此匹配大小写语句引发SyntaxError

number = 1

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

有什么办法能让这件事成功吗?

vsikbqxv

vsikbqxv1#

您可以使用this notebook

  • 复制一份
  • 运行第一个单元格
  • 重新加载(Ctrl + R或Cmd + R)
  • 运行第二单元
nhaq1z21

nhaq1z212#

您可以尝试以下命令:

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

然后

!update-alternatives --list python

这必须显示您下载的Python版本。
在那之后,

!sudo update-alternatives --config python

## !Set python3.10 as default.

最后,

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

然后在colab上检查默认Python版本。

!python3 --version

相关问题