I have an Amazon Elastic Compute Cloud (Amazon EC2) instance that runs on Amazon Linux 2. I want to create an isolated Python 3 virtual environment with the Boto 3 library on my instance.
Resolution
Install Python 3 for Amazon Linux 2
-
Use SSH to connect to your EC2 Linux instance. For more information, see Connect to your Linux instance from Linux or macOS using SSH.
-
Perform a yum check-update to refresh the package index. The check-update also looks for available updates. You don't need to update other packages to create the Python 3 environment.
-
To determine if your host already has Python 3 installed, run list installed:>
[ec2-user ~]$ yum list installed | grep -i python3
If Python 3 isn't installed, then you get the following output:
[ec2-user ~]$ yum list installed | grep -i python3[ec2-user ~]$
[ec2-user ~]$ python3
-bash: python3: command not found
If Python 3 is installed, then you get the following output:
[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
-
If Python 3 isn't installed, then 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
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, then 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
-
Attach an AWS Identity and Access Management (IAM) role to your EC2 instance with permissions policies for Boto 3 to interact with AWS APIs. For other authentication methods, see the Boto 3 documentation.
-
To activate the environment, source the activate file in the bin directory under your project directory:
[ec2-user ~]$ source ~/my_app/env/bin/activate(env) [ec2-user ~]$
-
Make sure that your environment has the latest pip module installed:
(env) [ec2-user ~]$ pip install pip --upgrade
-
To install the Boto 3 library within your virtual environment, use the pip command:
(env) [ec2-user ~]$ pip install boto3
-
Run Python:
(env) [ec2-user ~]$ pythonPython 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.
>>>>
-
Import the Boto 3 library, and then validate that it works. This step requires that you configured the permissions policies 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():
print(bucket.name)
>>> exit()
-
To exit the virtual environment, run the deactivate command:
(env) [ec2-user ~]$ deactivate
[ec2-user ~]$
-
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
-
Source the ~/.bashrc file in your home directory to reload your environment's bash environment. This 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 ~]$
Related information
Update instance software on your Amazon Linux instance
Launch an instance using the Launch Instance Wizard
Virtualenv on the Python Packaging Authority (PYPA) website