【问题标题】:How do I install Python in Google Cloud Shell?如何在 Google Cloud Shell 中安装 Python?
【发布时间】:2019-04-27 08:57:26
【问题描述】:

我在我的谷歌云 shell 上有 python 3.5 并且想要 3.7,所以我可以对要通过谷歌云功能部署的代码进行命令行调试(并使用 3.7 功能,例如 f-strings)。

我尝试了以下各种形式:

sudo apt-get install python37

永远回来

Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package python37

【问题讨论】:

  • 你试过python3.7吗?
  • 是的 - python3.7python3.7.3 - 结果都一样

标签: python python-3.x google-cloud-shell


【解决方案1】:

Python 3 可以作为安装 Conda/Anaconda 的副作用安装在 Cloud Shell 中。将链接复制到此处提供的所需安装程序 shell 脚本:Installing on Linux

示例

Welcome to Cloud Shell! Type "help" to get started.
$ wget https://repo.anaconda.com/miniconda/Miniconda3-py39_4.10.3-Linux-x86_64.sh
$ bash Miniconda3-py39_4.10.3-Linux-x86_64.sh

按照说明操作后,关闭 Cloud Shell 并打开一个新会话。 Python 现已更新。

安装 Conda 后,您现在可以为其他 Python 版本创建环境,如下所示:

$ conda create -n "py37" python=3.7.0
$ conda activate py37
$ python --version
Python 3.7.0

【讨论】:

  • 此解决方案的问题在于,正如您在输出中看到的那样,安装的是 py3.9.4,而不是请求的 3.7。
  • 很公平。但是使用conda的目的是能够随意使用不同的版本。
  • @abalter 查看更新的答案,展示如何在安装 Conda 后为不同版本的 Python 设置环境。
【解决方案2】:

另一种简单的方法是

sudo `which conda` install python=3.7 -y

【讨论】:

    【解决方案3】:
    
    # install pyenv to install python on persistent home directory
    curl https://pyenv.run | bash
    
    # add to path
    echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> ~/.bashrc
    echo 'eval "$(pyenv init -)"' >> ~/.bashrc
    echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
    
    # updating bashrc
    source ~/.bashrc
    
    # install python 3.7.4 and make default
    pyenv install 3.7.4
    pyenv global 3.7.4
    
    # execute
    python
    

    这是基于@yungchin 的回答。

    【讨论】:

    • 在运行pyenv之前我必须运行一个新的shell实例@
    • @Ali Khosro 这项技术是否适用于 Colab,它实际上是 3.6.9?
    • 当您不能(或不想)使用包管理器安装更新版本的 python 时,这应该适用于任何 linux 环境。
    • 它不起作用,在 'pyenv global' 之后,全局 Python 是旧的
    【解决方案4】:

    这在 GCP shell 上对我有用。

    # Install requirements
    sudo apt-get install -y build-essential checkinstall libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev zlib1g-dev openssl libffi-dev python3-dev python3-setuptools wget 
    
    # Prepare to build
    mkdir /tmp/Python37
    cd /tmp/Python37
    
    # Pull down Python 3.7, build, and install
    wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz
    tar xvf Python-3.7.0.tar.xz
    cd /tmp/Python37/Python-3.7.0
    ./configure
    sudo make altinstall
    

    那么你就可以这样调用 Python:

    python3.7 ./yourScript.py

    源:https://serverfault.com/questions/918335/best-way-to-run-python-3-7-on-ubuntu-16-04-which-comes-with-python-3-5

    【讨论】:

    • 成功了,谢谢。你知道我现在如何用它制作一个 virtualenv 吗?
    【解决方案5】:

    即使软件包可以通过 apt 获得,使用 apt 的不利之处在于,无论何时从 Cloud Shell 断开连接,您都必须重新安装:它总是会丢弃您的运行时容器。

    为了方便起见,我建议使用https://github.com/pyenv/pyenv。如果您遵循安装指南(并注意在我们的示例中,添加的 bash 配置文件应该进入 .bashrc),您最终会在您的主目录中生成一个 python 构建,该构建在 Cloud Shell 会话中保持不变。这只需几个步骤:

    1. 将repo克隆到~/.pyenv
    2. .bashrc 中添加三行(参见自述文件)以调整您的$PATH
    3. pyenv install 3.7.3 # 这需要一些时间来构建
    4. pyenv global 3.7.3 # 将此版本设置为默认版本

    【讨论】:

      猜你喜欢
      • 2019-02-12
      • 2021-06-03
      • 1970-01-01
      • 1970-01-01
      • 2020-09-22
      • 1970-01-01
      • 2018-04-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多