Pyrebase
Pyrebase copied to clipboard
OSError: Cannot load native module 'Crypto.Hash._SHA256'
When I try to run a python file that only has the line:
import pyrebase
I get the following issue:
Traceback (most recent call last):
File "analytics-suite.py", line 7, in <module>
import pyrebase
File "/Users/mdcopley/Documents/GitHub/example-pyrebase/env/lib/python3.5/site-packages/pyrebase/__init__.py", line 1, in <module>
from .pyrebase import initialize_app
File "/Users/mdcopley/Documents/GitHub/example-pyrebase/env/lib/python3.5/site-packages/pyrebase/pyrebase.py", line 17, in <module>
from oauth2client.service_account import ServiceAccountCredentials
File "/Users/mdcopley/Documents/GitHub/example-pyrebase/env/lib/python3.5/site-packages/oauth2client/service_account.py", line 25, in <module>
from oauth2client import client
File "/Users/mdcopley/Documents/GitHub/example-pyrebase/env/lib/python3.5/site-packages/oauth2client/client.py", line 47, in <module>
from oauth2client import crypt
File "/Users/mdcopley/Documents/GitHub/example-pyrebase/env/lib/python3.5/site-packages/oauth2client/crypt.py", line 55, in <module>
from oauth2client import _pycrypto_crypt
File "/Users/mdcopley/Documents/GitHub/example-pyrebase/env/lib/python3.5/site-packages/oauth2client/_pycrypto_crypt.py", line 16, in <module>
from Crypto.Hash import SHA256
File "/Users/mdcopley/Documents/GitHub/example-pyrebase/env/lib/python3.5/site-packages/Crypto/Hash/SHA256.py", line 55, in <module>
""")
File "/Users/mdcopley/Documents/GitHub/example-pyrebase/env/lib/python3.5/site-packages/Crypto/Util/_raw_api.py", line 168, in load_pycryptodome_raw_lib
raise OSError("Cannot load native module '%s'" % name)
OSError: Cannot load native module 'Crypto.Hash._SHA256'
I've researched this issue elsewhere, and I've tried switching my 64-bit python3 executable to a 32-bit, reinstalling dependencies, and nothing has resolved this issue yet.
Here is my setup information:
Python 3.5.3 (v3.5.3:1880cb95a742, Jan 16 2017, 08:48:58)
Pyrebase==3.0.27
Any help on what can resolve this issue will be greatly appreciated!
Hi @mcopley08 I'm getting this same error, how did you solve it?
I also have this error. I am trying to import pyrebase using AWS lambda handler function.
I'm facing this issue as well when importing pyrebase on AWS Lambda, please advise
This is how I solved it. I went into pyrebase.py and manually removed the dependency on Crypto. Required removing a single method for custom token creation. So, if you need custom token creation, that will break, but otherwise the error goes away.
Could someone please explain how to fix this package so it works on AWS Lambda? What needs to be installed or configured? Could we re-open this ticket please?
Same issue (running on AWS lambda). Any ideas?
Same issue also. I believe it's because Crypto doesn't support Python 3.6
I also see this issue when packaging with pyinstaller.
I ran into this issue a yesterday with Crypto on lambda (via zappa), and I didn't see anyone post a fix, but here's what I did -> The problem manifests itself in zappa because the native library is compiled on the client arch and uploaded - instead of on the lambda arch. As a quick solve, I downloaded an Amazon Linux vagrant box, shared the path on my machine to the box via config.vm.synced_folder, and then deployed to lambda through the vagrant box.
My comment above provides a way to make it work. I have successfully used pyrebase with AWS Lambda at this point.
It is possible that having pycryptodome in setup.py requirements is causing the problem. see #152
Good chance the Crypto you are looking at is not that of pycrypto as normally expected, but that of pycryptodome. And in worst scenario, having both in your project's dependencies have already corrupted your installation...
@wtesler Hi Will, following your advice, here's what I did:
- commented out
from Crypto.PublicKey import RSAin.requirements/pyrebase/pyrebase.py - re-deploy (I'm using serverless framework for deployment) with
sls deploy
But issue remains, is there anything that I missed? Thank you.
I know I'm really late to all of this - but I restarted my computer as a last resort and it started working on my local machine.
i commented from Crypto.PublicKey import RSA from pyrebase.py and i removed Crypto folder.
Has this been patched/an actual fix been found?
In my case, I remove the folder Crypto
and reinstall
pip install cryptography
And it works on Lambda
Did anyone fix this? I run the application from py file is ok. But when I build the app with pyinstaller, the binary file comes Cannot load native module 'Crypto.Hash._SHA256'
I had the same problem with serverless framework and lambda, and solved it building my serverless app in a linux environment. (i was using OSX before) Tested with Ubuntu 16.04
had the same issue with the Crypto.Hash._SHA256 module in the lambda function, using Python 3.7.
fixed that adding the library pycrypto, https://pypi.org/project/pycrypto/
For those of you having problems with the implementation on AWS Lambda:
The problem is most likely the difference between the runtime you are using to build your deployment package and the runtime used by your Lambda. In my case I was using python3.6 in my machine while having the python3.7 runtime in AWS.
To solve this without messing with your local version of Python, you can:
- Use Pipenv to create a runtime matching the one used in the Lambda and build your deployment package using the pipenv shell.
- Use the Serverless Framework and the serverless-python-requirements plugin to build and deploy your function.
I ran into this issue a yesterday with Crypto on lambda (via zappa), and I didn't see anyone post a fix, but here's what I did -> The problem manifests itself in zappa because the native library is compiled on the client arch and uploaded - instead of on the lambda arch. As a quick solve, I downloaded an Amazon Linux vagrant box, shared the path on my machine to the box via config.vm.synced_folder, and then deployed to lambda through the vagrant box.
works thanks
I received the same error. I figured the root cause. Putting it here hoping it would help somebody. In my case, I had originally created the python package for AWS lambda in ubuntu. The pycryptodome package installed the static for that required by the linux system (_SHA256.cpython-36m-x86_64-linux-gnu.so). These libraries has extension '.so'. Now, when I tried running the package on my windows machine it threw error since on windows it was looking for library compatible on windows system (_SHA256.cp36-win_amd64.pyd) and my package had the library for linux systems (_SHA256.cpython-36m-x86_64-linux-gnu.so).
Fixes:
For windows (to run locally):
- Create a virtual environment. pip install -r requirement.txt (this should install Crypto package from pycryptodome as it is required by pyrebase.)
- Now, you run the code locally and it should work.
For running code on AWS lambda:
Simple solution, switch to a linux machine. Create a virtual environment and install all the required files using pip install. Then use this to deploy on lambda.
If you don't have access to a linux system. Just pick the file from here (get "_SHA256.cpython-36m-x86_64-linux-gnu.so") and put in the folder Crypto/Hash/ folder.
If you arrive here because of AWS lambda runtime issue I would recommend doing the following:
- Use the Serverless Framework and the serverless-python-requirements plugin to build and deploy your function/layer
- Serverless will package your code locally and upload it. If you're not running on an amazon linux machine then you'll need to dockerize. I've used the following dockerfile to build my image then ran serverless from inside and the runtime worked:
FROM amazonlinux:2.0.20220218.1
RUN yum update -y && \
curl -fsSL https://rpm.nodesource.com/setup_17.x | bash - && \
yum install -y python3-pip python3-setuptools unzip git nodejs libffi-dev && \
pip3 install --upgrade awscli pip && \
npm install -g serverless && \
curl -s https://pyenv.run | bash
ENV PATH="/root/.pyenv/bin:$PATH"
RUN eval "$(pyenv init -)" && \
eval "$(pyenv virtualenv-init -)" && \
pip install --upgrade pip pipenv