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.
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
(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
$ sudo yum install -y python3
Confirmation after installation
$ python3 -V
Python 3.7.9
python
$ sudo amazon-linux-extras install -y python3.8
Confirmation after installation
$ python3.8 -V
Python 3.8.5
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)
Installing the package from pipenv will add the package to your Pipfile.
$ pipenv install numpy #Example of installing numpy
Pipfile
[packages]
numpy = "*"
$ pipenv install --dev autopep8 flake8
Pipfile
[dev-packages]
autopep8 = "*"
flake8 = "*"
If the package is managed by requirements.txt
, you can also install it with Pipenv from its contents.
$ pipenv install -r ./requirements.txt
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