AWS Lambda -- Unable to import srsly.ujson.ujson for SpaCy - python

I am trying to add SpaCy as a dependency to my Python Lambda. I am doing this by installing SpaCy as a standalone dependency inside a directory named dependencies using pip3 install spacy --no-deps -t . This is because I can't load the entire Spacy dependency inside the \tmp directory of my Lambda.
I am able to successfully upload the folder to s3 and download it during the Lambda invocation. When I try to import spacy, I get this error: [ERROR] Runtime.ImportModuleError: Unable to import module : No module named 'srsly.ujson.ujson'.
I manually installed srsly inside dependencies\ and I have all the files that are listed as per this link. This was referenced by this link. One of the responses says, "it seems like Python can't load it, because it's not compiled?". How would I compile a dependency which has a .c file in it?
One other question which I found on SO is this question, but I have already manually installed srsly. How to I import the module? Thanks.
I manually check in my code for the presence of ujson before importing spacy like this:
if os.path.exists('/tmp/dependencies/srsly/ujson/ujson.c'):
print('ujson exists')
and the print statement gets printed.

For me pip uninstalling and installing srsly again worked fine.. sometimes its just the compatibility issue with your python version so make sure correct python/srsly versions are present

Well it is a bit strange, but my solution for this problem was to create an aditional "ujson" folder in the srsly folder and then move all the ujson generated code to the folder "ujson" previously created

Related

Module Not Found Error For Custom Pypi Package

I am trying to upload my package to pypi and use it. Using Twine i upload it to pypi but when i try to use this package I get Module not found error.
My Error :
My Folder structure for the package is :
The error points to the modelpg/__init__.py , here's my modelpg/__init__.py file.
Is it due to my package name is same as .py file.
EDIT 1 :
My Transformer/__init__.py file
All of the imports in modelpg/__init__.py would need to either be relative:
from .Transformer.transformer import Transformer
or absolute, with the modelpg package name:
from modelpg.Transformer.transformer import Transformer
This has nothing to do with PyPI, by the way – the same applies even if the package wasn't installed from there.

Install a python module in the same directory as the main handler

Hello I want to install a module named python-ldap locally in the same directory as my main so that it could be zipped and uploaded as a standalone function. The reason is AWS Lambda doesn't support installing this module (but i have installed it successfully on AmazonLinux). So I'm hoping i can install the module in an AmazonLinux instance and zip it so it runs on any instance. If its possible that is.
For example purposes i have a folder deploy-ldap with a single lambda_function.py inside.
The lambda_function.py simply imports the module like so:
import ldap
def main():
print("Success")
What I tried so far:
There are some resources on this suggesting to copy a single .so file but it didn't work for me and resulted in an error where another .so.2 file is being requested
Furthermore i tried installing the module with pip install python-ldap -t . but this also resulted in an error: "Unable to import module 'lambda_function': No module named '_ldap'"
All input appreciated, thank you. ^^
The import python-ldap is incorrect because module names in Python shouldn't contain dashes. The correct import as in the examples should be:
import ldap
Then to make it available in the environment of your AWS Lambda function, you should follow the same steps as documented in Deployment package with dependencies which consists of the following steps:
Prepare the file containing the code
Perform pip install python-ldap
Deploy the code along with the installation to AWS Lambda

Issue importing Google Neuroglancer Package

I am using Google's Neuroglancer which I downloaded from GitHub and trying to run an example script provided by them. However, one of the lines is import neuroglancer, and since I cloned the whole repo there is a neuroglancer folder with all of the required files, but I am getting the following error:
ImportError: No module named 'neuroglancer'
Is there any way I could fix this? I don't see the issue since neuroglancer is in the same file path as the python script.
In case you are running the example in the neuroglancer project try following their guide.
Otherwise you might want to try and installing the Neuroglancer package using something like pip and a virtual environment to be able to import the package into the project.

Pysftp package is not working in lambda function throwing error : cannot import name '_bcrypt' from 'bcrypt' (./lib/bcrypt/__init__.py)

I download (pip install pysftp) and make a zip file and upload in a lambda function.
but it is not working in a lambda function. throwing error.
Response:
{
"errorMessage": "Unable to import module 'lambda_function': cannot import name '_bcrypt' from 'bcrypt' (./lib/bcrypt/__init__.py)",
"errorType": "Runtime.ImportModuleError"
}
Many thank you in advance.
The operation "pip install pysftp" is to be performed in a linux distribution for preparing the lib for aws lambda. I had used ubuntu in docker on windows to perform pip install on mounted volume to generate the lib.
Try reinstalling the packages and uploading the new packages. If it still shows error, move your development environment from Windows to Linux.
Similar kind of error for your reference: [1]: https://forums.aws.amazon.com/thread.jspa?messageID=804753&tstart=0
Since you need to troubleshoot module dependencies, the python runtime environment of AWS Lambda must be inspected.
In your AWS Lambda, print the modules that are loaded, and therefore available to the other modules that your code imports.
def lambda_handler(event, context):
print (help("modules"))
Running this in a python interpreter is illuminating.
python
help("modules")
You will see Please wait a moment while I gather a list of all available modules... and then a big list of available modules that are importable.
You will find that you are missing bcrypt, for within that module as taught by help(bcrypt) you will find the missing dependency _bcrypt.
Should bcrypt be available to the lambda or just a python interpreter, it is found in this manner.
>>> bcrypt._bcrypt
<module 'bcrypt._bcrypt' from '/usr/local/lib/python2.7/site-packages/bcrypt/_bcrypt.so'>
To install everything in the same directory, use the below command
pip install pysftp -t .
After that, zip the entire dir & upload to Lambda console.
Little old trick that helpssfor those who using Lambda for the 1st time...

Downloaded package ImportError

I have recently installed python 2.17.14 to use a package which I installed in the command prompt with:
python -m pip install packageName
However, whenever I try to use it with a script provided by the package authors, I get Import Errors:
ImportError: cannot import X from YX
ImportError: attempted relative import with no known parent package.
I don't know what I'm doing wrong, as I am really new to Python. Does anyone have any ideas?
The package is called neurodesign and I downloaded the try out script from the official website "neuropowertools.org"
Best,
Max
In case anyone (who is also new to python^^) fails ridiculously at this task as well: I had to manually install all the modules used within this package for it to work as they weren't installed automatically.

Categories