1
0

More than 3 years have passed since last update.

GCPのCloud ShellのPython2.7.16をPython3.7.3に移行する

Last updated at Posted at 2021-08-16

GCPでPython3が使いたい!

LINEBOTかLINE NotifyをGCPでPythonを定期で動かして作ろうとしていました。
そこでGCPのCloud Shellを使うことになりました。

Cloud ShellのPythonのバージョン確認

Cloud ShellのPythonのバージョンを確認してみると...

cloud_shell
$ python -V

********************************************************************************
Python 2 is deprecated. Upgrade to Python 3 as soon as possible.
See https://cloud.google.com/python/docs/python2-sunset

To suppress this warning, create an empty ~/.cloudshell/no-python-warning file.
The command will automatically proceed in  seconds or on any key.
********************************************************************************
Python 2.7.16

おっと!Python2.7.16...

そこでPythonのバージョンをあげるためにpyenvを導入します。

pyenvの導入

cloud_shell
$ curl https://pyenv.run | bash
$ pyenv init
$ echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(pyenv init -)"' >> ~/.bashrc
$ echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
$ pyenv install 3.7.3 #バージョンはお好きなものを
$ pyenv global 3.7.3
$ source ~/.bashrc

これでpyenvを無事に導入することができると思います。
また、早速pyenvを用いてPython 3.7.3をインストールできたと思います。
Pythonのバージョンを見てみましょう

Pythonのバージョン確認

cloud_shell
$ python -V
Python 3.7.3

無事にGCPでPython3をインストールすることができました!
ついでにpipをpip3に変えておきましょう。

cloud_shell
$ alias pip=pip3
$ pip --version
pip 18.1 from /usr/lib/python3/dist-packages/pip (python 3.7)

これでだいたいの環境が整いました。
GCPでもローカルと同様にPythonの環境構築を行なっておくとスムーズに開発ができます。

基礎部分しか環境構築していないので、あとはいつも通り個人で環境構築してみてください!

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0