41
30

More than 1 year has passed since last update.

AmazonLinux2でPython3環境構築

Last updated at Posted at 2020-12-24

AmazonLinux2には標準でPython2系がインストール済みですが、Python3を入れたい場合に共存してコマンドが面倒なので標準でPython3を使うように変更する手順です。
一旦python自体の仮想環境(pyenv)については記載していません。

環境情報

使用AMI-id,OS,カーネルバージョン
$ /opt/aws/bin/ec2-metadata
ami-id: ami-00f045aed21a55240

$ cat /etc/system-release
Amazon Linux 2

$ uname -a
Linux hogehuga.ap-northeast-1.compute.internal 4.14.209-160.335.amzn2.x86_64 #1 SMP Wed Dec 2 23:31:46 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
作業前python環境
$ python -V
Python 2.7.18

$ pip --version
bash: pip: command not found

$ pipenv --version
bash: pipenv: command not found

Python

各種パッケージ管理システムでのバージョンについて

(2020/12/24現在)amzn2-coreのyumリポジトリでのpythonマイナーバージョンは若干古いです。

通常のyum
$ yum info python3
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Available Packages
Name        : python3
Arch        : i686
Version     : 3.7.9
Release     : 1.amzn2.0.1
Size        : 72 k
Repo        : amzn2-core/2/x86_64
Summary     : Interpreter of the Python programming language
URL         : https://www.python.org/
License     : Python
Description : Python is an accessible, high-level, dynamically typed, interpreted programming
            : language, designed with an emphasis on code readability.
            : It includes an extensive standard library, and has a vast ecosystem of
            : third-party libraries.
            : 
            : The python3 package provides the "python3" executable: the reference
            : interpreter for the Python language, version 3.
            : The majority of its standard library is provided in the python3-libs package,
            : which should be installed automatically along with python3.
            : The remaining parts of the Python standard library are broken out into the
            : python3-tkinter and python3-test packages, which may need to be installed
            : separately.
            : 
            : Documentation for Python is provided in the python3-docs package.
            : 
            : Packages containing additional libraries for Python are generally named with
            : the "python3-" prefix.

Name        : python3
Arch        : x86_64
Version     : 3.7.9
Release     : 1.amzn2.0.1
Size        : 71 k
Repo        : amzn2-core/2/x86_64
Summary     : Interpreter of the Python programming language
URL         : https://www.python.org/
License     : Python
Description : Python is an accessible, high-level, dynamically typed, interpreted programming
            : language, designed with an emphasis on code readability.
            : It includes an extensive standard library, and has a vast ecosystem of
            : third-party libraries.
            : 
            : The python3 package provides the "python3" executable: the reference
            : interpreter for the Python language, version 3.
            : The majority of its standard library is provided in the python3-libs package,
            : which should be installed automatically along with python3.
            : The remaining parts of the Python standard library are broken out into the
            : python3-tkinter and python3-test packages, which may need to be installed
            : separately.
            : 
            : Documentation for Python is provided in the python3-docs package.
            : 
            : Packages containing additional libraries for Python are generally named with
            : the "python3-" prefix.

Extras LibraryであればPythonの3.8系が使用できます。

ExtrasLibrary
$ amazon-linux-extras list | grep python
 44  python3.8                available    [ =stable ]

Python3.7インストール

$ sudo yum install -y python3
インストール後確認
$ python3 -V
Python 3.7.9

Python3.8インストール

$ sudo amazon-linux-extras install -y python3.8
インストール後確認
$ python3.8 -V
Python 3.8.5

Python3.9インストール

3.9はyumでインストールできないのでソースをDLしてきてコンパイルします。

$ sudo yum install gcc openssl-devel bzip2-devel libffi-devel
$ cd /opt
$ sudo wget https://www.python.org/ftp/python/3.9.13/Python-3.9.13.tgz
$ sudo tar xzf Python-3.9.13.tgz
$ cd Python-3.9.13
$ sudo ./configure --enable-optimizations
$ sudo make altinstall
$ sudo rm -f /opt/Python-3.9.13.tgz
インストール後確認
$ python3.9 -V
Python 3.9.13

alias設定

Python3をインストールした状態だとPython -Vでバージョン確認しても以前Python 2.7が動作してしまいます。
毎回3.8と入力するのは手間なので、エイリアスを設定してpyhonコマンド実行時に使用されるバージョンを上書きします。

alias設定
$ echo 'alias python=python3.8' >> ~/.bashrc
$ source ~/.bashrc
動作バージョン確認
$ python -V
Python 3.8.5

pip

Python3をインストールした際一緒にインストールされています。ただしこちらも3.8等の接尾が必要です。

バージョン確認
$ pip --version
bash: pip: command not found

#python3.7の場合
$ pip3 --version

#python3.8の場合
$ pip3.8 --version
pip 9.0.3 from /usr/lib/python3.8/site-packages (python 3.8)

pipenv

Homebrewを使用する方法やpipを使用する方法、その他色々インストール方法はあるみたいですが、pipのみ記載しておきます。

インストール
$ sudo pip3.8 install pipenv
バージョン確認
$ pipenv --version
pipenv, version 2020.11.15

因みにpipenvをインストールすると前項でcommand not foundだったpipが効くようになります。

バージョン確認
$ pip --version
pip 20.3.3 from /usr/local/lib/python3.8/site-packages/pip (python 3.8)

パッケージのインストール

pipenvからパッケージをインストールするとPipfileにパッケージが追加されます.

$ pipenv install numpy    # numpyをインストールする例
Pipfile
[packages]
numpy = "*"

開発用パッケージのインストール

$ pipenv install --dev autopep8 flake8
Pipfile
[dev-packages]
autopep8 = "*"
flake8 = "*"

requirements.txtからのインストール

requirements.txtでパッケージが管理されている場合に、その内容からPipenvでインストールすることもできます。

$ pipenv install -r ./requirements.txt

参考

amazon linux2でPython2系からPython3系に切り替える手順 - がじぇったー
Amazon Linux 2 で Boto 3 ライブラリを使用して Python 3 仮想環境を作成する
Pipenvと仮想環境 — pipenv 2018.11.27.dev0 ドキュメント
Pipenvを用いたPythonの環境構築 - Qiita
Pipenvを使ったPython開発まとめ - Qiita
How to Install Python 3.9 on Amazon Linux 2 - TechAdmin

41
30
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
41
30