How do I create an isolated Python 3.4 environment with Boto 3 with an Amazon EC2 instance that's running Amazon Linux using virtualenv?

Last updated: 2019-02-21

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

Short Description

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

1.    Install Python 3.4 for Amazon Linux.

2.    Use virtualenv to create the Python environment.

3.    Activate the environment and install Boto 3.

Note: These instructions are for EC2 instances running Amazon Linux. If you're running Amazon Linux 2, see How do I create an isolated Python 3 environment with Boto 3 with an Amazon EC2 instance that's running Amazon Linux 2 using virtualenv?

Resolution

Install Python 3.4

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.4, if it's not already installed.

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

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

[ec2-user ~]$ which python34 
/usr/bin/python3.4

Use virtualenv to create the Python environment

1.    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
/home/ec2-user
[ec2-user ~]$ mkdir venv
[ec2-user ~]$ cd venv
[ec2-user ~]$ pwd
/home/ec2-user/venv

2.    Use the virtualenv command to create the python34 environment.

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

Activate the environment and install Boto 3

1.    Attach an IAM role to the 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 using the source command on the activate file in the bin directory, which is under your project directory.

[ec2-user ~]$ source /home/ec2-user/venv/python34/bin/activate

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

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

4.    Use the pip command to install Boto 3 from within the python34 environment.

[ec2-user ~]$ pip install boto3

5.    Run Python using the python executable.

[ec2-user ~]$ python
Python 3.4.3 (default, Apr 1 2015, 18:10:40)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] 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. The following 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():
print(bucket.name)
>>> exit()

7.    Deactivate the python34 environment.

[ec2-user ~]$ deactivate

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

[ec2-user ~]$ which python
/usr/bin/python

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

[ec2-user ~]$ echo "source /home/ec2-user/venv/python34/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 python34.

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

Did this article help you?

Anything we could improve?


Need more help?