I have an Amazon Elastic Compute Cloud (Amazon EC2) instance that runs on Amazon Linux 2 (AL2) or Amazon Linux 2023 (AL2023). I want to create an isolated Python 3 virtual environment with the AWS SDK for Python (Boto3) on my instance.
Resolution
Install Python 3
Complete the following steps:
-
Use SSH to connect to your EC2 Linux instance.
-
To refresh the package index, run the following command:
yum check-update
Note: The preceding command also looks for available updates. You don't need to update other packages to create the Python 3 environment.
-
To determine whether your host already has Python 3 installed, run the following command:
[ec2-user ~]$ yum list installed | grep -i python3
If Python 3 isn't installed, then you receive the following output:
[ec2-user ~]$ yum list installed | grep -i python3
[ec2-user ~]$ python3
-bash: python3: command not found
If Python 3 is installed, then you receive the following output based on your distribution:
AL2:
[ec2-user ~]$ yum list installed | grep -i python3
python3.x86_64 3.7.4-1.amzn2.0.4 @amzn2-core
python3-libs.x86_64 3.7.4-1.amzn2.0.4 @amzn2-core
python3-pip.noarch 9.0.3-1.amzn2.0.1 @amzn2-core
python3-setuptools.noarch 38.4.0-3.amzn2.0.6 @amzn2-core
[ec2-user ~]$ whereis python3
python3: //usr/bin/python3 /usr/bin/python3.7 /usr/bin/python3.7m /usr/lib/python3.7 /usr/lib64/python3.7 /usr/include/python3.7m /usr/share/man/man1/python3.1.gz
AL2023:
[ec2-user ~]$ yum list installed | grep -i python3
python3.x86_64 3.9.21-1.amzn2023.0.3 @System
python3-pip-wheel.noarch 21.3.1-2.amzn2023.0.11 @System
python3-libs.x86_64 3.9.21-1.amzn2023.0.3 @System
python3-setuptools.noarch 59.6.0-2.amzn2023.0.5 @System
[ec2-user ~]$ whereis python3
python3: /usr/bin/python3 /usr/share/man/man1/python3.1.gz
-
If Python 3 isn't installed, then run the following command to install the package with the yum package manager:
[ec2-user ~]$ sudo yum install python3 -y
Create a virtual environment under the ec2-user home directory
To create the app directory with the virtual environment inside of it, run the following command:
[ec2-user ~]$ python3 -m venv my_app/env
Note: Replace my_app with your application directory name.
Activate the virtual environment and install the Boto3 library
Complete the following steps:
-
Attach an AWS Identity and Access Management (IAM) role to your instance. The role must have a permissions policy that allows the SDK for Python (Boto3) to perform the actions that your configuration requires. For other authentication methods, see the Configuration on the Boto3 website.
-
To activate the environment, run the following command:
[ec2-user ~]$ source ~/my_app/env/bin/activate
(env) [ec2-user ~]$
Note: Replace my_app with your application directory name.
-
To make sure that your environment has the latest pip module installed, run the following command:
(env) [ec2-user ~]$ pip install pip --upgrade
-
To install the Boto3 library within your virtual environment, run the following command:
(env) [ec2-user ~]$ pip install boto3
-
To run Python, run the following command:
(env) [ec2-user ~]$ python
Python 3.7.4 (default, Dec 13 2019, 01:02:18)[GCC 7.3.1 20180712 (Red Hat 7.3.1-6)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>>
-
To import the Boto3 library and confirm that it works, run the following command:
>>> import boto3 # no error
The following example output lists all the Amazon Simple Storage Service (Amazon S3) buckets within the AWS account:
>>> import boto3 # no error
>>> s3 = boto3.resource('s3')
>>> for bucket in s3.buckets.all():
... print(bucket.name)
... # Press Enter twice to execute the loop
>>> exit() # Then type exit() to quit Python shell
-
To exit the virtual environment, run the following command:
(env) [ec2-user ~]$ deactivate
[ec2-user ~]$
-
To automatically activate the virtual environment when you log in, run the following command to update the ~/.bashrc file:
[ec2-user ~]$ echo "source ${HOME}/my_app/env/bin/activate" >> ${HOME}/.bashrc
Note: Replace my_app with your application directory name. The preceding command automatically activates your virtual environment.
-
To reload your environment's bash environment, run the following command:
[ec2-user ~]$ source ~/.bashrc
Example output:
(env) [ec2-user ~]$
Note: The (env) shows that you correctly activated your virtual environment. Now, you can use the Python environment by default in all future SSH sessions and when you run the source ~/bashrc command.
Related information
Update instance software on your AL2 instance
Launch an EC2 instance using the launch instance wizard in the console
Virtualenv on the Python Packaging Authority (PyPA) website