How do I install Python 3.7 in google cloud shell

元气小坏坏 提交于 2019-11-28 23:13:21

问题


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).

I try 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

Any help would be really appreciated!


回答1:


This worked for me on the 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:

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




回答2:


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.

I'd recommend using https://github.com/pyenv/pyenv for convenience. 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. This involves just a few steps:

  1. clone the repo into ~/.pyenv
  2. append three lines (see the README) to .bashrc to adjust your $PATH
  3. pyenv install 3.7.3 # this takes a while to build
  4. pyenv global 3.7.3 # sets this version as the default



回答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

# 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.



来源:https://stackoverflow.com/questions/53468831/how-do-i-install-python-3-7-in-google-cloud-shell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!