[英]how to change the python version from default 3.5 to 3.8 of google colab
我使用以下方法在 google colab 上下載了 python 3.8 版:
!apt-get install python3.8
現在我想將 python 在google colab
中使用的默認版本從3.6
更改為3.8
。 怎么做??
我讀過一些答案,但沒有更新......
Colab 具有默認 python 3.7 和替代 3.6(2021 年 7 月 26 日)
# Choose one of the given alternatives:
!sudo update-alternatives --config python3
# This one used to work but now NOT(for me)!
# !sudo update-alternatives --config python
# Check the result
!python3 --version
# Attention: Install pip (... needed!)
!sudo apt install python3-pip
試試這些命令
!update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1
然后
!update-alternatives --list python
這必須顯示您下載的 python 版本
在那之后
!sudo update-alternatives --config python
## !Set python3.8 as default.
最后
!sudo update-alternatives --set python /usr/bin/python3.8
然后在 colab 上檢查您的默認 python 版本
!python3 --version
有一種方法可以使用您想要的任何版本的 python,而無需在本地運行 kernel 或通過 ngrok 代理。
下載 colab 筆記本。 打開文本編輯器將 kernel 規范更改為:
"kernelspec": {
"name": "py38",
"display_name": "Python 3.8"
}
這與Javascript 、 Java和Golang使用的技巧相同。
然后將編輯后的筆記本上傳到 Google Drive。 在 Google Colab 中打開筆記本。 它找不到py38 kernel,所以它使用普通的python3 kernel。 您需要安裝 python 3.8、 google-colab
package 和ipykernel
以您在上面定義的名稱:“py38”:
!wget -O mini.sh https://repo.anaconda.com/miniconda/Miniconda3-py38_4.8.2-Linux-x86_64.sh
!chmod +x mini.sh
!bash ./mini.sh -b -f -p /usr/local
!conda install -q -y jupyter
!conda install -q -y google-colab -c conda-forge
!python -m ipykernel install --name "py38" --user
重新加載頁面,瞧,你可以測試版本是否正確:
import sys
print("User Current Version:-", sys.version)
在那里可以找到一個工作示例。
在我看來,沒有“好”的方法可以做到這一點。 您可以做的是用 shebang 行開始您的腳本。 shebang 行將為以下代碼設置 python 版本。 在這里找到一些相關的答案和信息。 如何告訴 Python 腳本使用特定版本
在此處查找有關如何在 colab 中使用 shebang 的一些信息。 https://colab.research.google.com/github/jhermann/blog/blob/master/_notebooks/2020-02-28-env_with_arguments.ipynb#scrollTo=SYv4FagrzLVu
當您有更多版本的 python 的腳本時,您可能會遇到此問題。 當 python 文件必須使用 #!/bin/env python 時,處理多個 python 版本
這是我成功嘗試過的:
!sudo apt-get update -y
!sudo apt-get install python3.8
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8
!update-alternatives --install /usr/bin/python python /usr/bin/python3.8
!update-alternatives --list python
!sudo update-alternatives --config python
!sudo update-alternatives --set python /usr/bin/python3.8
!python3 --version
這是我的解決方案,它完全改變了運行時版本,而不僅僅是解釋器:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.