【问题标题】:Is it possible to use Python 3.10 in Google Colab?是否可以在 Google Colab 中使用 Python 3.10?
【发布时间】:2022-01-05 12:15:21
【问题描述】:

我想在 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,因此 match case 语句引发 SyntaxError

number = 1

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

有什么办法可以做到吗?

【问题讨论】:

  • 仅仅因为系统上有 Python 3.10 并不意味着笔记本正在使用它。您是否告诉笔记本使用较新的 Python?
  • @MisterMiyagi 我不认为我做了,但我不知道怎么做,我使用的所有命令都在问题中描述,我该怎么办?

标签: python python-3.x google-colaboratory


【解决方案1】:

您可以尝试以下命令:

!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

【讨论】:

  • sys.version 在笔记本中显示了什么?
  • 更新 shell 命令使用的 Python 很简单(只需设置正确的 PATH)。问题是使笔记本使用正确的版本。
  • 这实际上是我已经做过的,正如我所说的,它不足以让 Python 3.10 代码在代码单元中工作,你可以看到 print(sys.version) 仍然输出 3.7.12。
猜你喜欢
  • 1970-01-01
  • 2019-07-26
  • 2021-11-07
  • 2020-04-19
  • 2020-12-16
  • 2020-10-23
  • 2019-02-25
  • 2019-08-03
  • 2022-01-18
相关资源
最近更新 更多