简体   繁体   English

如何将 python 版本从默认的 3.5 更改为 google colab 的 3.8

[英]how to change the python version from default 3.5 to 3.8 of google colab

I downloaded python version 3.8 on google colab using:我使用以下方法在 google colab 上下载了 python 3.8 版:

!apt-get install python3.8

Now I want to change the default python version used in google colab uses from 3.6 to 3.8 .现在我想将 python 在google colab中使用的默认版本从3.6更改为3.8 how to do it??怎么做??

I have read few ans but there are no updates...我读过一些答案,但没有更新......

Colab has default python 3.7 and alternative 3.6 (on 26.07.2021) 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

try these commands试试这些命令

!update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1

then然后

!update-alternatives --list python

this must display your downloaded python version这必须显示您下载的 python 版本

after that在那之后

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

finally最后

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

then check your default python version on colab然后在 colab 上检查您的默认 python 版本

!python3 --version

There is a way to use any version of python you want, without having to run a kernel locally or going through an ngrok proxy.有一种方法可以使用您想要的任何版本的 python,而无需在本地运行 kernel 或通过 ngrok 代理。

Download the colab notebook.下载 colab 笔记本。 Open a text editor to change the kernel specification to:打开文本编辑器将 kernel 规范更改为:

"kernelspec": {
  "name": "py38",
  "display_name": "Python 3.8"
}

This is the same trick as the one used with Javascript , Java , and Golang .这与JavascriptJavaGolang使用的技巧相同。

Then upload the edited notebook to Google Drive.然后将编辑后的笔记本上传到 Google Drive。 Open the notebook in Google Colab.在 Google Colab 中打开笔记本。 It cannot find the py38 kernel, so it use normal python3 kernel.它找不到py38 kernel,所以它使用普通的python3 kernel。 You need to install a python 3.8, the google-colab package and the ipykernel under the name you defined above: "py38":您需要安装 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

Reload the page, and voilà, you can test the version is correct:重新加载页面,瞧,你可以测试版本是否正确:

import sys
print("User Current Version:-", sys.version)

A working example can be found there .那里可以找到一个工作示例。

In my opinion there is no "good" way to do this.在我看来,没有“好”的方法可以做到这一点。 What you can do is start your script with a shebang line.您可以做的是用 shebang 行开始您的脚本。 A shebang line will set the python version for the following code. shebang 行将为以下代码设置 python 版本。 Find some related answers and informations here.在这里找到一些相关的答案和信息。 How do I tell a Python script to use a particular version 如何告诉 Python 脚本使用特定版本

Find here some informations on how to use shebang in colab.在此处查找有关如何在 colab 中使用 shebang 的一些信息。 https://colab.research.google.com/github/jhermann/blog/blob/master/_notebooks/2020-02-28-env_with_arguments.ipynb#scrollTo=SYv4FagrzLVu https://colab.research.google.com/github/jhermann/blog/blob/master/_notebooks/2020-02-28-env_with_arguments.ipynb#scrollTo=SYv4FagrzLVu

When you have script for more versions of python you might come across this issue.当您有更多版本的 python 的脚本时,您可能会遇到此问题。 Dealing with multiple python versions when python files have to use #!/bin/env python 当 python 文件必须使用 #!/bin/env python 时,处理多个 python 版本

This is what I have tried with success:这是我成功尝试过的:

!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

Here's my solution which completely changes the runtime version, not just the interpreter:这是我的解决方案,它完全改变了运行时版本,而不仅仅是解释器:

https://stackoverflow.com/a/74538231/9738112 https://stackoverflow.com/a/74538231/9738112

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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