Deana talks you through setting up
an isolated Python 3.4 environment using
virtualenv

python-boto3-virtualenv

How do I create an isolated Python 3.4 environment with Boto 3 on Amazon EC2 using virtualenv?

These instructions are for an EC2 instance running Amazon Linux.

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. Amazon Linux comes with Python 2.7 installed and enabled by default. You can launch Python to see the default version: 

[ec2-user ~]$ which python

/usr/bin/python

[ec2-user ~]$ python

Python 2.7.10 (default, Dec 8 2015, 18:25:23)

[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>>

4. Amazon Linux also comes with Boto for Python 2 installed:

>>> import boto            # no errors

>>> exit()

5. Install Python 3.4:

[ec2-user ~]$ sudo yum install python34

Confirm the install was successful:

[ec2-user ~]$ which python3.4

/usr/bin/python3.4

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

7. Amazon Linux comes with virtualenv already installed. 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.

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

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

9. You are now referencing the new environment.

[ec2-user ~]$ which python

~/venv/python34/bin/python   # python34 is now the default

10. Install Boto 3 from within the python34 environment: 

[ec2-user ~]$ pip install boto3

11. Run Python using the python3 executable. You can also import the Boto 3 SDK:

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

>>> import boto3           # no error

>>> exit()

12. Deactivate the python34 environment with the following command:

[ec2-user ~]$ deactivate

13. Run the "which" command to confirm you are using the default environment: 

[ec2-user ~]$ which python

/usr/bin/python

14. To activate the python34 environment automatically when you log in, add it to your .bashrc file:

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


Did this page help you? Yes | No

Back to the AWS Support Knowledge Center

Need help? Visit the AWS Support Center.

Published: 2016-06-24

Updated: 2018-03-26