Georgi Koemdzhiev
Georgi Koemdzhiev

Reputation: 11931

ModuleNotFoundError - How do I install Python3 packages so my my user-data script get's executed?

I am trying to run a python script in EC2 user-data. The EC2 instance I am launching uses a custom AMI image that I repaired. I installed the 2 packages that I need - boto3 and pyodbc packages by executing these commands (notice, I am installing those as root):

sudo yum -y install boto3 pyodbc 

My user-data script:

#!/bin/bash
set -e -x

# set AWS region
echo "export AWS_DEFAULT_REGION=${region}" >> /etc/profile
source /etc/profile
# copy python script from the s3 bucket
aws s3 cp s3://${bucket_name}/ /home/ec2-user --recursive

/usr/bin/python3 /home/ec2-user/my_script.py

After launching a new EC2 Instance (using the my custom AMI) and checking /var/log/cloud-init-output.log I see that error:

+ python3 /home/ec2-user/main_rigs_stopgap.py
Traceback (most recent call last):
  File "/home/ec2-user/my_script.py", line 1, in <module>
    import boto3
ModuleNotFoundError: No module named 'boto3'
util.py[WARNING]: Failed running /var/lib/cloud/instance/scripts/part-001 [1]
cc_scripts_user.py[WARNING]: Failed to run module scripts-user (scripts in /var/lib/cloud/instance/scripts)
util.py[WARNING]: Running module scripts-user (<module 'cloudinit.config.cc_scripts_user' from '/usr/lib/python2.7/site-packages/cloudinit/config/cc_scripts_user.pyc'>) failed

Any suggestions, please?

Upvotes: 0

Views: 1047

Answers (1)

sahinakkaya
sahinakkaya

Reputation: 6056

To make sure that you installed modules to correct version of python, use builtin pip module of the python version you are using:

/usr/bin/python3 -m pip install module_name

Upvotes: 1

Related Questions