Search code examples
pythonamazon-web-servicesaws-lambdacloud9-ide

Error when trying to run import pytube with aws lambda


Hello I'm trying to import pytube in my lambda function but i get the following error when I try to run the code:

Response { "errorMessage": "Unable to import module 'index'" }

Function Logs Unable to import module 'index': No module named 'pytube'

Here is my code:

import boto3
from pytube import YouTube

s3 = boto3.resource('s3')

URL = ''
PATH = ''

def handler(event, context):
    YouTube(URL).streams.first().download(PATH)

And my IDE says pytube is already installed:

Requirement already satisfied: pytube in /home/ec2-user/.local/lib/python2.7/site-packages

What could be the problem?


Solution

  • The problem is that you are not including your dependencies in your deployment package.

    See https://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html#deployment-pkg-for-virtualenv for full details, but effectively you must do:

    pip install pytube -t /path/to/project-dir
    

    Before making the zip file for uploading.

    (Note that boto3 is an exception to this rule, as AWS automatically include it for you, on the assumption that your lambda probably works with AWS services!)