如何在Google Cloud Shell中安装Python 3.7

时间:2018-11-25 15:08:55

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

我在Google Cloud Shell上安装了python 3.5,并且希望使用3.7,所以我可以对将要通过google Cloud函数进行部署的代码进行命令行调试(并使用3.7功能,例如f字符串)。

我尝试以下各种形式:

sudo apt-get install python37

总是回来

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

任何帮助将不胜感激!

5 个答案:

答案 0 :(得分:5)

这在GCP shell上对我有用。

# Install requirements
sudo apt-get install -y build-essential
sudo apt-get install -y checkinstall
sudo apt-get install -y libreadline-gplv2-dev
sudo apt-get install -y libncursesw5-dev
sudo apt-get install -y libssl-dev
sudo apt-get install -y libsqlite3-dev
sudo apt-get install -y tk-dev
sudo apt-get install -y libgdbm-dev
sudo apt-get install -y libc6-dev
sudo apt-get install -y libbz2-dev
sudo apt-get install -y zlib1g-dev
sudo apt-get install -y openssl
sudo apt-get install -y libffi-dev
sudo apt-get install -y python3-dev
sudo apt-get install -y python3-setuptools
sudo apt-get install -y 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

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

答案 1 :(得分:1)


#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

# install python 3.7.4 and make default
pyenv install 3.7.4
pyenv global 3.7.4

# execute
python

这是基于@yungchin的答案。

答案 2 :(得分:0)

即使这些软件包可以通过apt获得,使用apt的弊端也在于,无论何时与Cloud Shell断开连接,您都必须重新安装一次:

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

  1. 将存储库克隆到~/.pyenv
  2. .bashrc上附加三行(请参阅自述文件)以调整$PATH
  3. pyenv install 3.7.3#这需要一段时间才能构建
  4. pyenv global 3.7.3#将此版本设置为默认版本

答案 3 :(得分:0)

另一种简单的方法是

sudo ``which conda`` install python=3.7 -y

很明显,我的意思是 conda 周围的单个反引号,但是我不记得如何在github markdown中转义反引号。

Slash escape is \应该工作,但不能工作

<pre>标签中:

`Slash escape is \`supposed\` to work, but doesn't`

答案 4 :(得分:0)

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 现已更新。