How do I import a Google Module directly in AWS Lambda? - python

I am trying to process an audio file from my S3 through a Lambda function which should call the google speech api to to speech-to-text with the file. However, it doesn't seem like Lambda has this module installed.
Is there a way to import the module externally/manually?
import json
import boto3
import array as arr
import botocore
from botocore.vendored import requests
import os
import urllib
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types
def lambda_handler(event, context):
output = 'test'
return output
Expected is that the import works, however the error output is the following:
"errorMessage": "Unable to import module 'lambda_function': No module
named 'google'",

You have to add the google library to your deployment package. Depending on your deployment process, there's various ways how to go about it. The documentation is a good place to start learning about that.
BTW, instead of Google's speech-to-text you can use the one from AWS called Transcribe. You don't have to install any additional library to use that, boto3 will do.

Check the python version used for installing the dependencies and give the same runtime in aws Lambda. I used python 3.8 for installing and gave 3.7 in runtime and ran into this error.

A better way would be to create a file called requirements.txt and add all the dependencies in there
google-cloud-speech==0.36.0

On my understanding, this problem could be solved by adding the required modules as a Layer in Lambda. Then it will be available to your script.
Also, that Layer could be re-used for other Lambdas in the future.

Related

Google Cloud Functions can't make "import io"

I try to launch Google Cloud Function.
io lib is used:
from io import BytesIO
But when I try to add 'io' to requirements.txt function deploy with error.
The mistake happen only when I add this string to the file.
How I can fix it?

Solve Unable to import module 'lambda_function': cannot import name '_AES'

I am trying to deploy my lambda function that makes a requests and decrypts data using pycrypto's module Crypto.Cipher AES. This is how my import looks:
import boto3
from botocore.vendored import requests
import gzip
from io import StringIO, BytesIO
import base64
import sys
from datetime import datetime
import time
from Crypto.Cipher import AES
I run my code in my local Mac Environment and it works perfectly, but when I upload my package to AWS Lambda and test it, I get the following error:
Unable to import module 'lambda_function': cannot import name '_AES'
I checked this question and downloaded the pycrypto package from this git repo, made the build and copied the results to my lambda folder, packaged it and still didn't work.
I checked the result of the build and I noticed that within this folder:
pycrypto-2.6.1/build/lib.linux-x86_64-3.7/Crypto/Cipher
The AES files are generated with an extension related to my Mac OS
I tried building the package on a Linux EC2 instance, but still get the same error (although the files change)
I tried a new path based on the following question but still failed with the exact same error.
"errorMessage": "Unable to import module 'lambda_function'"
Why is AWS Lambda not able to read the AES module in the pycrypto package? I have deployed Lambda function with other external libraries and never faced this issue.
This worked for me now using python2:
https://www.github.com/Doerge/awslambda-pycrypto
I just downloaded this project and zipped my lambda_function.py file with both the Crypto and pycrypto-2.6.1.dist-info folders.
I see that the compiled .so files in Crypto/Cipher/ (like _AES.so) lack the python version and OS architecture and distribution that mine had (i.e: AES.cpython-27m-x86_64-linux-gnu.so). I will update my answer if I find a way to build the package properly by myself rather than using a third person compiled library.

ImportError: cannot import name 'enums'

I am trying to use Google Speech API to recognize speech from mic input in real time. I have tried https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/speech/cloud-client/transcribe_streaming_mic.py
but this error came out. Anybody knows how to solve this?
from google.cloud.speech import enums
ImportError: cannot import name 'enums'
Update:
I have solved the problem by running the code in virtual environment as suggested by the official Cloud Speech API website. However, I still dont understand why we need to run that in virtual environment instead of my original environment as the virtual environment is not Linux or other OS.
I am using cloud function to transcribe the audio to text.
I believe the documentation for google-cloud-speech has been updated, you may refer to the documentation. https://pypi.org/project/google-cloud-speech/. I believe the class is now called speech_v1, you can use the alias "as speech" to make the github sample codes work.
from google.cloud import speech_v1 as speech
from google.cloud.speech_v1 import enums
from google.cloud.speech_v1 import types
HTH.
I got mine working with the following import
from google.cloud.speech_v1.gapic import enums
Using pip install google.cloud.speech, you are getting the latest version, currently V2.
In V2, enums and types have been removed and are no longer needed.
https://github.com/googleapis/python-speech/blob/master/UPGRADING.md#enums-and-types

Can I import a library in zapier code

Is there a way to import a library in Zapier Python Code?
Specifically, I want the 'datasift' library.
When I try, I just get "ImportError: No module named datasift"
In the zapier documentation, it specifically says that it only allows for the requests import and standard python imports
We can import only requests, so we need to find another way to run any python code which include the other libraries.
Maybe, we can build python to exe file and run it directly by zapier service.

import clientsecrets ImportError: No module named 'clientsecrets'

I have installed google-api-python-client library using pip3.4 Iam following tutorial from google authenicating client library so every time i run the python code provided by google to authenticate client library throws error as
import clientsecrets ImportError: No module named 'clientsecrets'
I have only python3.4 running on my pc and no other python. it might be similar thread to one here Python can't find module 'clientsecrets' when trying to set up oauth2 in Django using the Google Python API
but i found no solution there.
The python code i got from google is here
https://developers.google.com/maps/documentation/tracks/auth#authenticating_using_a_client_library
The library doesn't support Python 3. As of PEP 404:
In Python 3, implicit relative imports within packages are no longer available - only absolute imports and explicit relative imports are supported.
The oauth2client library uses import clientsecrets, which for Python 3 would need to be rewritten as from . import clientsecrets. Even if you changed those, the rest of the library would still be incompatible with Python 3.
There's at least one fork for Python 3 development, but it doesn't seem to be a big priority for the project. Until then, you'll have to find another library or write a wrapper yourself with requests for the functionality you need.

Categories