如何在Google Cloud Shell中安装Python 3.7

Ric*_*ard 10 python-3.x google-cloud-shell

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

我尝试以下各种形式:

sudo apt-get install python37
Run Code Online (Sandbox Code Playgroud)

总是回来

Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package python37
Run Code Online (Sandbox Code Playgroud)

任何帮助将非常感激!

Ali*_*sro 16


# 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
Run Code Online (Sandbox Code Playgroud)

这是基于@yungchin 的回答。


小智 7

这在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
Run Code Online (Sandbox Code Playgroud)

然后,您可以像这样调用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


yun*_*hin 5

即使可以通过apt使用这些软件包,使用apt的缺点是,每当您与Cloud Shell断开连接时,都必须重新安装一次:它始终会丢弃运行时容器。

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

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