Pyrebase icon indicating copy to clipboard operation
Pyrebase copied to clipboard

OSError: Cannot load native module 'Crypto.Hash._SHA256'

Open mcopley08 opened this issue 8 years ago • 23 comments

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!

mcopley08 avatar Mar 17 '17 04:03 mcopley08

Hi @mcopley08 I'm getting this same error, how did you solve it?

darrenatyurgosky avatar Apr 15 '17 18:04 darrenatyurgosky

I also have this error. I am trying to import pyrebase using AWS lambda handler function.

fuithecat avatar Apr 22 '17 09:04 fuithecat

I'm facing this issue as well when importing pyrebase on AWS Lambda, please advise

pprajoth avatar May 26 '17 19:05 pprajoth

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.

wtesler avatar Jun 03 '17 07:06 wtesler

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?

skunkwerk avatar Jun 18 '17 23:06 skunkwerk

Same issue (running on AWS lambda). Any ideas?

matmoody avatar Jun 29 '17 05:06 matmoody

Same issue also. I believe it's because Crypto doesn't support Python 3.6

wurambo avatar Jun 29 '17 20:06 wurambo

I also see this issue when packaging with pyinstaller.

Hypertron avatar Jul 15 '17 07:07 Hypertron

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.

ryankanno avatar Jul 20 '17 19:07 ryankanno

My comment above provides a way to make it work. I have successfully used pyrebase with AWS Lambda at this point.

wtesler avatar Jul 28 '17 07:07 wtesler

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...

ippeiukai avatar Aug 02 '17 15:08 ippeiukai

@wtesler Hi Will, following your advice, here's what I did:

  1. commented out from Crypto.PublicKey import RSA in .requirements/pyrebase/pyrebase.py
  2. re-deploy (I'm using serverless framework for deployment) with sls deploy

But issue remains, is there anything that I missed? Thank you.

genkio avatar Aug 10 '17 11:08 genkio

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.

mcopley08 avatar Aug 15 '17 21:08 mcopley08

i commented from Crypto.PublicKey import RSA from pyrebase.py and i removed Crypto folder.

andreabisello avatar Sep 29 '17 13:09 andreabisello

Has this been patched/an actual fix been found?

sillmnvg avatar Dec 06 '17 19:12 sillmnvg

In my case, I remove the folder Crypto and reinstall pip install cryptography

And it works on Lambda

koorukuroo avatar Jul 03 '18 00:07 koorukuroo

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'

txhai avatar Sep 08 '18 12:09 txhai

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

jecel0911 avatar Dec 11 '18 22:12 jecel0911

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/

ffiore1 avatar Jun 02 '19 09:06 ffiore1

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:

Trowsing avatar Nov 03 '19 15:11 Trowsing

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

dw-sharon avatar Oct 02 '20 06:10 dw-sharon

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):

  1. Create a virtual environment. pip install -r requirement.txt (this should install Crypto package from pycryptodome as it is required by pyrebase.)
  2. 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.

bot2x avatar Jan 09 '21 15:01 bot2x

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

Fran-Rg avatar Mar 07 '22 14:03 Fran-Rg