简体   繁体   English

如何在 Google Cloud Shell 中安装 Python?

[英]How do I install Python in Google Cloud Shell?

I have python 3.5 on my google cloud shell and want 3.7 so I can do command line debugging of code I am going to deploy via google cloud functions (and use 3.7 features such as f-strings).我的 google cloud shell 上有 python 3.5 并且想要 3.7,这样我就可以对将要通过 google cloud 函数部署的代码进行命令行调试(并使用 3.7 功能,例如 f-strings)。

I have tried various forms of the following:我尝试了以下各种形式:

sudo apt-get install python37

and always get back并且总是回来

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

# 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

This is based on @yungchin answer.这是基于@yungchin 的回答。

This worked for me on the GCP shell.这在 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

Then you would just call Python like so:然后你可以像这样调用 Python:

python3.7 ./yourScript.py

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

Even if the packages were available through apt, the downside of using apt would be that you'd have to install all over again whenever you'd been disconnected from Cloud Shell: it always discards your runtime container.即使软件包可通过 apt 获得,使用 apt 的缺点是,只要您与 Cloud Shell 断开连接,就必须重新安装:它总是会丢弃您的运行时容器。

I'd recommend using https://github.com/pyenv/pyenv for convenience.为方便起见,我建议使用https://github.com/pyenv/pyenv If you follow the installation guide (and note the bash profile additions should go into .bashrc in our case) you end up with a python build in your home directory, which is persisted across Cloud Shell sessions.如果您按照安装指南进行操作(并注意,在我们的示例中,bash 配置文件添加内容应进入.bashrc ),您最终会在主目录中生成一个 python 版本,该版本会在 Cloud Shell 会话中持续存在。 This involves just a few steps:这仅涉及几个步骤:

  1. clone the repo into ~/.pyenv将 repo 克隆到~/.pyenv
  2. append three lines (see the README) to .bashrc to adjust your $PATH.bashrc添加三行(请参阅自述文件)以调整您的$PATH
  3. pyenv install 3.7.3 # this takes a while to build pyenv install 3.7.3 # 这需要一段时间来构建
  4. pyenv global 3.7.3 # sets this version as the default pyenv global 3.7.3 # 将此版本设为默认

另一种简单的方法是

sudo `which conda` install python=3.7 -y

Python 3 can be installed in Cloud Shell as a side-effect of installing Conda/Anaconda. Python 3 可以作为安装 Conda/Anaconda 的副作用安装在 Cloud Shell 中。 Copy the link to the desired installer shell script available here: Installing on Linux .将链接复制到此处可用的所需安装程序 shell 脚本: 在 Linux 上安装

Example例子

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

After following the instructions, close Cloud Shell and open a new session.按照说明操作后,关闭 Cloud Shell 并打开一个新会话。 Python is now updated. Python现已更新。

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

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