How do I create an isolated Python 3 environment with Boto 3 on an Amazon Elastic Compute Cloud (Amazon EC2) instance that's running Amazon Linux 2 using virtualenv?

To create an isolated Python environment for an Amazon EC2 instance running Amazon Linux 2, you need to:

1.    Install Python 3 for Amazon Linux 2.

2.    Install virtualenv, and then create the Python environment.

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

Note: These instructions are for EC2 instances running Amazon Linux 2. If you're running an earlier version of Amazon Linux, see the instructions for creating an isolated Python 3.4 environment.

Install Python 3

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

2.    Perform a yum install update.

[ec2-user ~]$ sudo yum update

3.    Launch Python, and then use the version command to find what Python versions are installed.

[ec2-user ~]$ python --version

4.    Install Python 3, if it's not already installed.

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

5.    Use the which command to confirm that the install was successful.

[ec2-user ~]$ which python3 

Install virtualenv and create the python3 environment

1.    Install virtualenv for the current user using pip3.

pip3 install --user virtualenv

2.    Create a directory to hold your virtualenv environments, and then use the cd command to make it your current directory. In the following example, the environments are stored in the venv directory, under the ec2-user directory.

[ec2-user ~]$ pwd
[ec2-user ~]$ mkdir venv
[ec2-user ~]$ cd venv
[ec2-user ~]$ pwd

3.    Use the virtualenv command to create the python3 environment.

[ec2-user ~]$ virtualenv -p /usr/bin/python3 python3
Running virtualenv with interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in python3/bin/python3
Also creating executable in python3/bin/python
Installing setuptools, pip...done.

Activate the environment and install Boto 3

1.    Attach an IAM role to your EC2 instance with the proper permission 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 /home/ec2-user/venv/python3/bin/activate

3.    Use the which command to verify that you are now referencing the new environment.

[ec2-user ~]$ which python
~/venv/python3/bin/python   # python3 is now the default

4.    Use the pip3 command to install Boto 3 from within the python3 environment.

[ec2-user ~]$ pip3 install boto3

5.    Run Python using the python executable.

[ec2-user ~]$ python
Python 3.7.0rc1 (default, Aug 1 2018, 03:51:55)
[GCC 7.3.1 20180303 (Red Hat 7.3.1-5)] on linux
Type "help", "copyright", "credits" or "license" for more information.

6.    Import the Boto 3 SDK, and then validate that it works. Be sure that you have the permission policies configured from step 1. This example lists all the Amazon Simple Storage Service (Amazon S3) buckets in your account.

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

7.    Deactivate the python3 environment.

[ec2-user ~]$ deactivate

8.    Run the which command to confirm that you are using the default environment.

[ec2-user@ip-172-31-28-44 ~]$ which python

9.    To activate the python3 environment automatically when you log in, add it to your .bashrc file.

[ec2-user ~]$ echo "source /home/ec2-user/venv/python3/bin/activate" >> /home/ec2-user/.bashrc

10.    Exit from the session.

11.    Re-connect to your EC2 instance, and then use the which command to verify that the default python version for your user is python3.

(python34) [ec2-user ~]$ which python

Did this page help you? Yes | No

Back to the AWS Support Knowledge Center

Need help? Visit the AWS Support Center

Published: 2019-02-14