簡體   English   中英

如何在 Google Cloud Shell 中安裝 Python?

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

我的 google cloud shell 上有 python 3.5 並且想要 3.7,這樣我就可以對將要通過 google cloud 函數部署的代碼進行命令行調試(並使用 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

# 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 的回答。

這在 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

即使軟件包可通過 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 # 將此版本設為默認

另一種簡單的方法是

sudo `which conda` install python=3.7 -y

Python 3 可以作為安裝 Conda/Anaconda 的副作用安裝在 Cloud Shell 中。 將鏈接復制到此處可用的所需安裝程序 shell 腳本: 在 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現已更新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM