rajeevpunna
rajeevpunna

Reputation: 163

How to install pip library in ec2-instance virtual environment using user data?

enter image description hereI am try to install pip libraries in ec2-instances virtual environment using user data.

But while I am trying that I will create at instance level but not at virtual environment level.

What I need exactly is will I am creating ec2-instance on configuration level I want to define that to create pip libraries at virtual environment level.

#!/usr/bin/env bash
sudo yum update
sudo yum install python3 pip3 -y
sudo pip3 install --upgrade pip 
pip install --upgrade pip
python3 -m venv /home/ec2-user/vertual-Environment
pip install django
pip install djangorestframework
pip install pillow 

Upvotes: 1

Views: 3577

Answers (2)

Charlesthk
Charlesthk

Reputation: 9684

Using user data, your should install your dependencies this way :

python3 -m venv /home/ec2-user/vertual-Environment
/home/ec2-user/vertual-Environment/bin/pip install django
/home/ec2-user/vertual-Environment/bin/pip install djangorestframework
/home/ec2-user/vertual-Environment/bin/pip install pillow

When using userdata, your script are executed independently, regardless of what have been done in previous executions.

Upvotes: 0

Marcin
Marcin

Reputation: 238071

You have to source your python env envrionment.

Example (I have verified now the code on Amazon Linux 2):

#!/usr/bin/env bash
sudo yum update -y
sudo yum install python3 python3-pip -y
sudo pip3 install --upgrade pip 
pip install --upgrade pip
python3 -m venv /home/ec2-user/vertual-Environment

# source your new python env
source /home/ec2-user/vertual-Environment/bin/activate

pip install django
pip install djangorestframework
pip install pillow 

Upvotes: 2

Related Questions