Building a Python3 environment with Amazon Linux2

Python2 system is already installed in Amazon Linux2 as standard, but if you want to install Python3, it coexists and the command is troublesome, so it is a procedure to change to use Python3 as standard. Once, the virtual environment (pyenv) of python itself is not described.

Environmental information

AMI used-id,OS,Kernel version


$ /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

Before work python environment


$ python -V
Python 2.7.18

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

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

Python

About versions in various package management systems

(As of 12/24/2020) The python minor version of amzn2-core in the yum repository is a bit old.

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

If it is Extras Library, you can use Python 3.8 series.

ExtrasLibrary


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

Python 3.7 installation

python


$ sudo yum install -y python3

Confirmation after installation


$ python3 -V
Python 3.7.9

Python 3.8 installation

python


$ sudo amazon-linux-extras install -y python3.8

Confirmation after installation


$ python3.8 -V
Python 3.8.5

alias setting

With Python 3 installed, Python 2.7 still works even if I check the version with Python -V. Entering 3.8 every time is a hassle, so set an alias to overwrite the version used when running the pyhon command.

alias setting


$ echo 'alias python=python3.8' >> ~/.bashrc
$ source ~/.bashrc

Operation version check


$ python -V
Python 3.8.5

pip It is installed when you install Python3. However, this also requires a suffix such as 3.8.

Version confirmation


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

#python3.In case of 7
$ pip3 --version

#python3.In case of 8
$ pip3.8 --version
pip 9.0.3 from /usr/lib/python3.8/site-packages (python 3.8)

pipenv There seems to be a method using Homebrew, a method using pip, and various other installation methods, but only pip is described.

Installation


$ sudo pip3.8 install pipenv

Version confirmation


$ pipenv --version
pipenv, version 2020.11.15

By the way, if you install pipenv, pip that was command not found in the previous section will work.

Version confirmation


$ pip --version
pip 20.3.3 from /usr/local/lib/python3.8/site-packages/pip (python 3.8)

Package installation

Installing the package from pipenv will add the package to your Pipfile.

$ pipenv install numpy    #Example of installing numpy

Pipfile


[packages]
numpy = "*"

Installation of development packages

$ pipenv install --dev autopep8 flake8

Pipfile


[dev-packages]
autopep8 = "*"
flake8 = "*"

Installation from requirements.txt

If the package is managed by requirements.txt, you can also install it with Pipenv from its contents.

$ pipenv install -r ./requirements.txt

reference

Procedure to switch from Python2 series to Python3 series on amazon linux2 --- Gajetta Create a Python 3 virtual environment using the Boto 3 library on Amazon Linux 2 (https://aws.amazon.com/jp/premiumsupport/knowledge-center/ec2-linux-python3-boto3/) Pipenv and Virtual Environment — pipenv 2018.11.27.dev0 Documentation Building a Python environment using Pipenv-Qiita Python development summary using Pipenv --Qiita

Recommended Posts

Building a Python3 environment with Amazon Linux2
Building a virtual environment with Python 3
[Pyenv] Building a python environment with ubuntu 16.04
Building a Python 3.6 environment with Windows + PowerShell
[Linux] Build a Docker environment with Amazon Linux 2
Building a python environment with virtualenv and direnv
Building a Python environment with WLS2 + Anaconda + PyCharm
Building a Python virtual environment
Building a Python virtual environment
Procedure for building a kube environment on amazon linux2 (aws) ~ (with bonus)
Recommendation of building a portable Python environment with conda
conda memorandum: Building a Python environment with supercomputer ITO
How to build a Python environment on amazon linux 2
Building a kubernetes environment with ansible 2
Prepare pipenv environment with amazon Linux 2
Building a Python environment on Ubuntu
Create a virtual environment with Python!
Building a kubernetes environment with ansible 1
Introduce Python 3.5.2 environment on Amazon Linux
[Python] Building an environment with Anaconda [Mac]
[Python] Create a virtual environment with Anaconda
[Mac] Building a virtual environment for Python
[Linux] Build a jenkins environment with Docker
Build a python virtual environment with pyenv
Build a modern Python environment with Neovim
Building a Python development environment for AI development
Building a Windows 7 environment for getting started with machine learning with Python
Python environment with docker-compose
Create a Python environment
Virtual environment with Python 3.6
Build a python environment with ansible on centos6
Building an Anaconda environment for Python with pyenv
Create a virtual environment with conda in Python
[Python] Build a Django development environment with Docker
Create a python3 build environment with Sublime Text3
[Python] Web development preparation (building a virtual environment)
A memo when creating a python environment with miniconda
Think about building a Python 3 environment in a Mac environment
Commands for creating a python3 environment with virtualenv
Work in a virtual environment with Python virtualenv.
Build a Python environment with OSX El capitan
Quickly build a Python Django environment with IntelliJ
Building a Python environment on a Sakura VPS server
Build a Python machine learning environment with a container
Build a python execution environment with VS Code
Get a quick Python development environment with Poetry
Run a batch of Python 2.7 with nohup on Amazon Linux AMI on EC2
Building a Python environment for pyenv, pyenv-virtualenv, Anaconda (Miniconda)
Write about building a Python environment for writing Qiita Qiita
Install Python environment with Anaconda
Manage python environment with virtualenv
Building a Docker working environment for R and Python
Build a python virtual environment with virtualenv and virtualenvwrapper
Build a python environment for each directory with pyenv-virtualenv
Create a python development environment with vagrant + ansible + fabric
Build python3 environment with ubuntu 16.04
I made a Python3 environment on Ubuntu with direnv.
Prepare python3 environment with Docker
Build python environment with direnv
Building an environment for natural language processing with Python
Build a machine learning application development environment with Python