How do I create a Python 3 virtual environment with the Boto 3 library on Amazon Linux 2?

Last updated: 2020-02-07

How do I create an isolated Python 3 virtual environment with the Boto 3 library on an Amazon Elastic Compute Cloud (Amazon EC2) instance or on-premise solution that's running Amazon Linux 2?

Short Description

To create an isolated Python environment for Amazon Linux 2, you must:

1.    Install Python 3 for Amazon Linux 2.

2.    Install a virtual environment under the ec2-user home directory.

3.    Activate the environment, and then install Boto 3.


Install Python 3 for Amazon Linux 2

1.    Connect to your EC2 Linux instance using SSH. For more information, see Connecting to your Linux instance using SSH.

2.    Perform a yum check-update to refresh the package index. The check-update also looks for available updates. Updating other packages shouldn't be required to create the Python 3 environment.

3.    Run list installed to determine if Python 3 is already installed on the host.

[ec2-user ~]$ yum list installed | grep -i python3

Python 3 not installed output example:

[ec2-user ~]$ yum list installed | grep -i python3
[ec2-user ~]$

[ec2-user ~]$ python3
-bash: python3: command not found

Python 3 already installed output example:

[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

4.    If Python 3 isn't already installed, then install the package using the yum package manager.

[ec2-user ~]$ sudo yum install python3 -y

Create a virtual environment under the ec2-user home directory

The following command creates the app directory with the virtual environment inside of it. You can change my_app to another name. If you change my_app, make sure that you reference the new name in the remaining resolution steps.

[ec2-user ~]$ python3 -m venv my_app/env

Activate the virtual environment and install Boto 3

1.    Attach an AWS Identity and Access Management (IAM) role to your EC2 instance with the proper permissions policies so that Boto 3 can interact with the AWS APIs. For other authentication methods, see the Boto 3 documentation.

2.    Activate the environment by sourcing the activate file in the bin directory under your project directory.

[ec2-user ~]$ source ~/my_app/env/bin/activate
(env) [ec2-user ~]$

3.    Make sure that you have the latest pip module installed within your environment.

(env) [ec2-user ~]$ pip install pip --upgrade

4.    Use the pip command to install the Boto 3 library within our virtual environment.

(env) [ec2-user ~]$ pip install boto3

5.    Run Python using the python executable.

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

6.    Import the Boto 3 library, and then validate that it works. This step requires that you have the permissions policies configured from step 1. The following example output lists all the Amazon Simple Storage Service (Amazon S3) buckets within the account.

>>> import boto3           # no error
>>> s3 = boto3.resource('s3')
>>> for bucket in s3.buckets.all():
>>> exit()

7.    Use the deactivate command to exit the virtual environment.

(env) [ec2-user ~]$ deactivate

[ec2-user ~]$

8.    To activate the virtual environment automatically when you log in, add it to the ~/.bashrc file.

[ec2-user ~]$ echo "source ${HOME}/my_app/env/bin/activate" >> ${HOME}/.bashrc

9.    Source the ~/.bashrc file in your home directory to reload your environment's bash environment. Reloading automatically activates your virtual environment. The prompt reflects the change (env). This change also applies to any future SSH sessions.

[ec2-user ~]$ source ~/.bashrc

(env) [ec2-user ~]$