Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mcopley08 opened this issue Mar 17, 2017 · 23 comments
Open

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

mcopley08 opened this issue Mar 17, 2017 · 23 comments

Comments

@mcopley08
Copy link

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!

@darrenatyurgosky
Copy link

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

@fuithecat
Copy link

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

@pprajoth
Copy link

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

@wtesler
Copy link

wtesler commented Jun 3, 2017

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.

@skunkwerk
Copy link

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?

@thisbejim thisbejim reopened this Jun 19, 2017
@matmoody
Copy link

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

@wurambo
Copy link

wurambo commented Jun 29, 2017

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

@Hypertron
Copy link

I also see this issue when packaging with pyinstaller.

@ryankanno
Copy link

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.

@wtesler
Copy link

wtesler commented Jul 28, 2017

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

@ippeiukai
Copy link

ippeiukai commented Aug 2, 2017

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

@genkio
Copy link

genkio commented Aug 10, 2017

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

@mcopley08
Copy link
Author

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.

@andreabisello
Copy link

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

@sillmnvg
Copy link

sillmnvg commented Dec 6, 2017

Has this been patched/an actual fix been found?

@koorukuroo
Copy link

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

And it works on Lambda

@txhai
Copy link

txhai commented Sep 8, 2018

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'

@jecel0911
Copy link

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

@ffiore1
Copy link

ffiore1 commented Jun 2, 2019

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/

@Trowsing
Copy link

Trowsing commented Nov 3, 2019

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:

@dw-sharon
Copy link

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

@bot2x
Copy link

bot2x commented Jan 9, 2021

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.

@Fran-Rg
Copy link

Fran-Rg commented Mar 7, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests