pyca / cryptography

cryptography is a package designed to expose cryptographic primitives and recipes to Python developers.
https://cryptography.io
Other
6.67k stars 1.53k forks source link

cannot import name 'exceptions' from 'cryptography.hazmat.bindings._rust' (unknown location) #9060

Closed mdevore300 closed 1 year ago

mdevore300 commented 1 year ago

I can find the file but it cant. Conda 4.10.1 Python 3.8.8 Setuptools 52.0.0 cryptography 41.0.0

alex commented 1 year ago

Please provide steps to reproduce, you've given us no information.

mdevore300 commented 1 year ago

Sorry, I am trying to use PDFPlumber in the code. All i did was run import pdfplumber and it gives the error

whatcould commented 1 year ago

I was having this same issue; anytime I try to requires something it fails with pyo3_runtime.PanicException: Python API call failed, when running line 16 from cryptography.hazmat.bindings._rust import _openssl, openssl.

I upgraded cffi and the issue seemed to resolve. I think for me it had something to do with compilation issues on Apple silicon, because I was getting this FFI issue.

mdevore300 commented 1 year ago

I am on Windows. I will see if that can solve it.

reaperhulk commented 1 year ago

To help you we need a method to reproduce this error in a fresh environment. Do you get this error in a new virtual environment?

mdevore300 commented 1 year ago

yes. I am in spyder, i highlighted import pdfplumber, hit F9 to run it and the error produces.

alex commented 1 year ago

You need to give us instructions on how you have installed cryptography. Spyder is a text editor, which I am not familiar with, if it's installing things for you, you'll need to do some research on how its doing so, as we're not familiar with it.

mdevore300 commented 1 year ago

It was pip installed or conda installed.

alex commented 1 year ago

Please understand, we cannot attempt to help debug things if you can't even definitely tell us how something was installed. Our package has millions of downloads a day which are successful, so this issue is somehow specific to your environment. We're trying to identify what feature of your environment leads to that, but you're providing us no information to go on. Please see https://cryptography.io/en/latest/faq/#what-issues-can-you-help-with-in-your-issue-tracker

mdevore300 commented 1 year ago

It was pip installed. Pip install cryptography in anaconda powershell

mdevore300 commented 1 year ago

The only thing possible i can think of is pyopenssl is 20.0.1, Maybe an update to it?

reaperhulk commented 1 year ago

Please create a new virtual environment and install your packages into it. It's not possible for us to give additional advice since this appears to be specific to your installation rather than a general issue.

github-actions[bot] commented 1 year ago

This issue has been waiting for a reporter response for 3 days. It will be auto-closed if no activity occurs in the next 5 days.

QuentinBennett commented 1 year ago

Please don't close, I'm getting the same error. I'll try to reduce to a simple test case. . . . PS C:\python> py Python 3.11.3 (tags/v3.11.3:f3909b8, Apr 4 2023, 23:49:59) [MSC v.1934 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information.

from cryptography.hazmat.primitives import padding

ModuleNotFoundError: No module named '_cffi_backend' thread '' panicked at 'Python API call failed', C:\Users\runneradmin.cargo\registry\src\index.crates.io-6f17d22bba15001f\pyo3-0.18.3\src\err\mod.rs:790:5 stack backtrace: 0: 0x7ff898eb0a12 - PyInitrust 1: 0x7ff898ec023b - PyInitrust 2: 0x7ff898eaea4a - PyInitrust 3: 0x7ff898eb075b - PyInitrust 4: 0x7ff898eb2709 - PyInitrust 5: 0x7ff898eb238b - PyInitrust 6: 0x7ff898eb2e38 - PyInitrust 7: 0x7ff898e42159 - PyInitrust 8: 0x7ff898e42129 - PyInitrust 9: 0x7ff899106c7d - PyInitopenssl 10: 0x7ff898e46461 - PyInitrust 11: 0x7ff898d107a8 - PyInitrust 12: 0x7ff898d03710 - PyInitrust 13: 0x7ff898e43797 - PyInitrust 14: 0x7ff898cf4043 - PyInit__rust 15: 0x7ff8a70375ac - Py_fopen_obj 16: 0x7ff8a703744d - Py_fopen_obj 17: 0x7ff8a70373a1 - Py_fopen_obj 18: 0x7ff8a6f9a69c - PyObject_Call 19: 0x7ff8a6f9a34b - PyObject_Call 20: 0x7ff8a6f9a0be - PyList_AsTuple 21: 0x7ff8a6f4edc8 - PyEval_EvalFrameDefault 22: 0x7ff8a6f6d644 - PyObject_GetBuffer 23: 0x7ff8a6f5e85b - PyObject_CallMethodObjArgs 24: 0x7ff8a6f5e796 - PyObject_CallMethodObjArgs 25: 0x7ff8a6f5e601 - PyConfig_FromDict 26: 0x7ff8a6f78a71 - PyImport_ImportModuleLevelObject 27: 0x7ff8a6f8884d - Py_GetConfig 28: 0x7ff8a6f50fff - PyEval_EvalFrameDefault 29: 0x7ff8a6f5fa77 - PyMapping_Check 30: 0x7ff8a6f5f137 - PyEval_EvalCode 31: 0x7ff8a6f5e3d4 - PyConfig_FromDict 32: 0x7ff8a6f5e2a4 - PyConfig_FromDict 33: 0x7ff8a6f96cec - PyUnicode_RichCompare 34: 0x7ff8a6f9a34b - PyObject_Call 35: 0x7ff8a6f9a0be - PyList_AsTuple 36: 0x7ff8a6f4edc8 - PyEval_EvalFrameDefault 37: 0x7ff8a6f6d644 - PyObject_GetBuffer 38: 0x7ff8a6f5e85b - PyObject_CallMethodObjArgs 39: 0x7ff8a6f5e796 - PyObject_CallMethodObjArgs 40: 0x7ff8a6f5e601 - PyConfig_FromDict 41: 0x7ff8a6f78a71 - PyImport_ImportModuleLevelObject 42: 0x7ff8a6f8884d - Py_GetConfig 43: 0x7ff8a6f50fff - PyEval_EvalFrameDefault 44: 0x7ff8a6f5fa77 - PyMapping_Check 45: 0x7ff8a6f5f137 - PyEval_EvalCode 46: 0x7ff8a6f5e3d4 - PyConfig_FromDict 47: 0x7ff8a6f5e2a4 - PyConfig_FromDict 48: 0x7ff8a6f96cec - PyUnicode_RichCompare 49: 0x7ff8a6f9a34b - PyObject_Call 50: 0x7ff8a6f9a0be - PyList_AsTuple 51: 0x7ff8a6f4edc8 - PyEval_EvalFrameDefault 52: 0x7ff8a6f6d644 - PyObject_GetBuffer 53: 0x7ff8a6f5e85b - PyObject_CallMethodObjArgs 54: 0x7ff8a6f5e796 - PyObject_CallMethodObjArgs 55: 0x7ff8a6f5e601 - PyConfig_FromDict 56: 0x7ff8a6f78a71 - PyImport_ImportModuleLevelObject 57: 0x7ff8a6fef568 - PyArg_ParseStackAndKeywords 58: 0x7ff8a6f96cec - PyUnicode_RichCompare 59: 0x7ff8a6f9a34b - PyObject_Call 60: 0x7ff8a6f9a0be - PyList_AsTuple 61: 0x7ff8a6f4edc8 - PyEval_EvalFrameDefault 62: 0x7ff8a6f6d644 - PyObject_GetBuffer 63: 0x7ff8a6f5e85b - PyObject_CallMethodObjArgs 64: 0x7ff8a6f5e796 - PyObject_CallMethodObjArgs 65: 0x7ff8a6f78a4b - PyImport_ImportModuleLevelObject 66: 0x7ff8a6f8884d - Py_GetConfig 67: 0x7ff8a6f50fff - PyEval_EvalFrameDefault 68: 0x7ff8a6f5fa77 - PyMapping_Check 69: 0x7ff8a6f5f137 - PyEval_EvalCode 70: 0x7ff8a6f5d80a - PyMapping_Items 71: 0x7ff8a6f5d786 - PyMapping_Items 72: 0x7ff8a70aa17e - PyThread_tss_is_created 73: 0x7ff8a6f133a5 - PyRun_SimpleFileObject 74: 0x7ff8a700a620 - PyRun_AnyFileObject 75: 0x7ff8a700aaef - Py_MakePendingCalls 76: 0x7ff8a700ab5f - Py_MakePendingCalls 77: 0x7ff8a700b964 - Py_RunMain 78: 0x7ff8a700b7f5 - Py_RunMain 79: 0x7ff8a6f560d9 - Py_Main 80: 0x7ff7cd311230 - 81: 0x7ff9526626ad - BaseThreadInitThunk 82: 0x7ff9537ca9f8 - RtlUserThreadStart Traceback (most recent call last): File "C:\python\ex.py", line 1, in from cryptography.hazmat.primitives import padding File "C:\python\Lib\site-packages\cryptography\hazmat\primitives\padding.py", line 11, in from cryptography.exceptions import AlreadyFinalized File "C:\python\Lib\site-packages\cryptography\exceptions.py", line 9, in from cryptography.hazmat.bindings._rust import exceptions as rust_exceptions pyo3_runtime.PanicException: Python API call failed

QuentinBennett commented 1 year ago

Please don't close, I'm getting the same error. I'll try to reduce to a simple test case. . . . PS C:\python> py Python 3.11.3 (tags/v3.11.3:f3909b8, Apr 4 2023, 23:49:59) [MSC v.1934 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information.

Answer was from https://stackoverflow.com/questions/34370962/no-module-named-cffi-backend

pip install cffi --upgrade

mdevore300 commented 1 year ago

I am working on it. Because of the system it is on, everything needs to get approval from IT, including updating python pkgs.

github-actions[bot] commented 1 year ago

This issue has been waiting for a reporter response for 3 days. It will be auto-closed if no activity occurs in the next 5 days.

github-actions[bot] commented 1 year ago

This issue has not received a reporter response and has been auto-closed. If the issue is still relevant please leave a comment and we can reopen it.

mdevore300 commented 1 year ago

So, i've updated all dependencies finally. Still getting the same error. So all i was trying to do was in spyder, import pdfplumber so i could run a script. So I highlighted import pdfplumber, then hit F9 The full traceback is as follows:

File "", line 1, in import pdfplumber

File "C:\ProgramData\Anaconda3\lib\site-packages\pdfplumber__init__.py", line 14, in for .pdf import PDF

File "C:\ProgramData\Anaconda3\lib\site-packages\pdfplumber\pdf.py, line 8, in from pdfminer.layour import LAParams

File "C:\ProgramData\Anaconda3\lib\site-packages\pdfminer\layout.py", line 20, in from .pdfinterp import Color

File "C:\ProgramData\Anaconda3\lib\site-packages\pdfminer\pdfinterp.py", line 12, in from .pdfdevice import PDFDevice

FIle "C:\ProgramData\Anaconda3\lib\site-packages\pdfminer\pdfdevice.py," line 17, in from .pdfpage import PDFPage

FIle "C:\ProgramData\Anaconda3\lib\site-packages\pdfminer\pdfpage.py," line 7, in from .pdfdocument import PDFDocument, PDFTextExtractionNotAllowed, PDFNoPageLabels

FIle "C:\ProgramData\Anaconda3\lib\site-packages\pdfminer\pdfdocument.py", line 23, in from cryptography.hazmat.preimitives.cipher import Cipher, algorithms, modes

FIle "C:\ProgramData\Anaconda3\lib\site-packages\cryptography\hazmat\primitives\ciphers__init__.py", line 11, in from cryptography.hazmat.primitives.ciphers.base import (

FIle "C:\ProgramData\Anaconda3\lib\site-packages\cryptography\hazmat\primitives\ciphers\base.py", line 10, in from cryptography.exceptions.import (

FIle "C:\ProgramData\Anaconda3\lib\site-packages\cryptography\exceptions.py", line 9, in from cryptography.hazmat.bindings._rust import exceptions as rust_exceptions

ImportError: cannot import name 'exceptions' from 'cryptography.hazmat.bindings._rust (unknown location)

rlandge146 commented 1 year ago

Has anybody able to resolve the issue..I am getting the following error when installing the same module in amazon glue..

ModuleNotFoundError: No module named 'cryptography.hazmat.bindings._rust'.I tried installing the following packages

cryptography, pyopenssl, setuptools-rust, pdfplumber as indicated in other threads

mdevore300 commented 1 year ago

I had to go back versions of cryptography and pdfplumber

rlandge146 commented 1 year ago

@mdevore300 which versions of cryptography and pdfplumber did you go back to to be able to resolve this issue.

mdevore300 commented 1 year ago

pdfplumber 0.9.0, cryptography 3.4.7

rlandge146 commented 1 year ago

These versions does not seem to work for me in amazon glue

SujayRhombuz commented 1 year ago

"cannot import name 'exceptions' from 'cryptography.hazmat.bindings._rust' (unknown location)",

have tried the option suggested above nothing works, i am trying to install AWS Lambda.

ascopes commented 10 months ago

Any update on this? x86_64 Python3.12, cryptography-41.0.7, cffi-1.16.0 on AWS Lambda is still producing this error, rendering the runtime unusable. Python 3.11 fails due to glibc mismatches even when using the manylinux 2014 distribution, so we are forced to use Python 3.12 for this.

AdelinaK commented 10 months ago

I'm using Python 3.11 and Pulumi to deploy Azure Functions. I have the same error: Failure Exception: ImportError: cannot import name 'x509' from 'cryptography.hazmat.bindings._rust' (unknown location)

tomy128 commented 10 months ago

@ascopes @AdelinaK downgrade to cryptography==3.4.8 will solve the issue. though i don't know why 🥲

ascopes commented 10 months ago

Assuming that defaults to an older manylinux version?

I've had to resort to building and installing all lambda artifacts in a manylinux container to work around issues with 3.11. Haven't tried 3.12 yet.

githubakash commented 9 months ago

I am also facing the same issue while deploying to AWS Lambda. I am using python 3.12 with fastapi = 0.99.0. Earlier I was getting the error as

"Unable to import module 'main': cannot import name 'exceptions' from 'cryptography.hazmat.bindings._rust' (unknown location)"

. after I downgrade my cryptography pkg to cryptography==3.4.8 , now I am getting the below error,

"errorMessage": "Unable to import module 'main': No module named 'cryptography.hazmat.bindings._openssl'",

I am new to python and seems, it is an rabbit-hole where we need to keep track of these dependencies which is nothing but an nightmare. has anyone got any solution to this? Also, I am not using cryptography pkg at all but it is getting installed as an dependencies to other pkg. Is there a way, I can get rid of this pkg all together??

mfouladi commented 8 months ago

Ran into the same issue on my Windows machine while working with fastapi and python-jose to add auth to my app. What ultimately worked for me was running: pip install --force-reinstall cryptography which pulled the correct versions of cffi for my machine which addressed the issue ModuleNotFoundError: No module named '_cffi_backend'

I did also purge my cache prior to this and updated pip: pip cache purge pip install --upgrade pip

still not sure why uninstalling and reinstalling the requirements or specific packages didnt work initially for me so doesn't feel completely resolved but force reinstall did end up working.

techtana0x commented 8 months ago

I was getting this issue as well on my M1 Mac v14.3.1

I had cffi==1.16.0 installed (by default)

I managed to get my django server running by manually installing cffi==1.14.0. I then tried installing cffi==1.15.0 and cffi==1.16.0 manually and my server still started.

Can't give you a reason why, but hope this helps anyone else.

ImportError: dlopen(/Users/name/Work/venvs/server/lib/python3.8/site-packages/_cffi_backend.cpython-38-darwin.so, 0x0002): symbol not found in flat namespace '_ffi_prep_closure' thread '<unnamed>' panicked at /Users/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/pyo3-0.18.3/src/err/mod.rs:790:5: Python API call failed note: run with RUST_BACKTRACE=1 environment variable to display a backtrace Traceback (most recent call last): File "manage.py", line 24, in <module> main() File "manage.py", line 20, in main execute_from_command_line(sys.argv) File "/Users/name/Work/venvs/server/lib/python3.8/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line utility.execute() File "/Users/name/Work/venvs/server/lib/python3.8/site-packages/django/core/management/__init__.py", line 382, in execute settings.INSTALLED_APPS File "/Users/name/Work/venvs/server/lib/python3.8/site-packages/django/conf/__init__.py", line 102, in __getattr__ self._setup(name) File "/Users/name/Work/venvs/server/lib/python3.8/site-packages/django/conf/__init__.py", line 89, in _setup self._wrapped = Settings(settings_module) File "/Users/name/Work/venvs/server/lib/python3.8/site-packages/django/conf/__init__.py", line 217, in __init__ mod = importlib.import_module(self.SETTINGS_MODULE) File "/opt/homebrew/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1014, in _gcd_import File "<frozen importlib._bootstrap>", line 991, in _find_and_load File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 671, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 843, in exec_module File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "/Users/name/Work/server/config/settings/__init__.py", line 10, in <module> import boto3 File "/Users/name/Work/venvs/server/lib/python3.8/site-packages/boto3/__init__.py", line 16, in <module> from boto3.session import Session File "/Users/name/Work/venvs/server/lib/python3.8/site-packages/boto3/session.py", line 17, in <module> import botocore.session File "/Users/name/Work/venvs/server/lib/python3.8/site-packages/botocore/session.py", line 30, in <module> import botocore.credentials File "/Users/name/Work/venvs/server/lib/python3.8/site-packages/botocore/credentials.py", line 34, in <module> from botocore.config import Config File "/Users/name/Work/venvs/server/lib/python3.8/site-packages/botocore/config.py", line 16, in <module> from botocore.endpoint import DEFAULT_TIMEOUT, MAX_POOL_CONNECTIONS File "/Users/name/Work/venvs/server/lib/python3.8/site-packages/botocore/endpoint.py", line 22, in <module> from botocore.awsrequest import create_request_object File "/Users/name/Work/venvs/server/lib/python3.8/site-packages/botocore/awsrequest.py", line 25, in <module> import botocore.utils File "/Users/name/Work/venvs/server/lib/python3.8/site-packages/botocore/utils.py", line 33, in <module> import botocore.httpsession File "/Users/name/Work/venvs/server/lib/python3.8/site-packages/botocore/httpsession.py", line 17, in <module> from urllib3.contrib.pyopenssl import orig_util_SSLContext as SSLContext File "/Users/name/Work/venvs/server/lib/python3.8/site-packages/urllib3/contrib/pyopenssl.py", line 50, in <module> import OpenSSL.crypto File "/Users/name/Work/venvs/server/lib/python3.8/site-packages/OpenSSL/__init__.py", line 8, in <module> from OpenSSL import SSL, crypto File "/Users/name/Work/venvs/server/lib/python3.8/site-packages/OpenSSL/SSL.py", line 10, in <module> from OpenSSL._util import ( File "/Users/name/Work/venvs/server/lib/python3.8/site-packages/OpenSSL/_util.py", line 6, in <module> from cryptography.hazmat.bindings.openssl.binding import Binding File "/Users/name/Work/venvs/server/lib/python3.8/site-packages/cryptography/hazmat/bindings/openssl/binding.py", line 15, in <module> from cryptography.exceptions import InternalError File "/Users/name/Work/venvs/server/lib/python3.8/site-packages/cryptography/exceptions.py", line 9, in <module> from cryptography.hazmat.bindings._rust import exceptions as rust_exceptions pyo3_runtime.PanicException: Python API call failed

abhinavbandaru commented 8 months ago

Anyone able to resolve the above issue?

I'm trying to use cryptography==42.0.5 with python==3.11.4, cffi==1.16.0rc2 and I'm facing this issue I have also tried downgrading python==3.10.4 and cffi==1.15.0 and still facing the same issue

File "/dbc/sc-dbc2162/abandaru/abandaru_py311/bora/install/vcsa-installer/vcsaCliInstaller/deps/cryptography/cryptography/hazmat/bindings/openssl/binding.py", line 15, in <module>
    from cryptography.exceptions import InternalError
File "/dbc/sc-dbc2162/abandaru/abandaru_py311/bora/install/vcsa-installer/vcsaCliInstaller/deps/cryptography/cryptography/exceptions.py", line 9, in <module>
    from cryptography.hazmat.bindings._rust import exceptions as rust_exceptions
ImportError: cannot import name 'exceptions' from 'cryptography.hazmat.bindings._rust' (/dbc/sc-dbc2162/abandaru/abandaru_py311/testing/vcsa-cli-installer/lin64/lib/library.zip/cryptography/hazmat/bindings/_rust/__init__.pyc)

I was using cryptography==38.0.4, python==3.10.4 and cffi==1.15.0 earlier and it was working fine If anyone has a fix for this, please do let me know

tomy128 commented 8 months ago

Anyone able to resolve the above issue?

I'm trying to use cryptography==42.0.5 with python==3.11.4, cffi==1.16.0rc2 and I'm facing this issue I have also tried downgrading python==3.10.4 and cffi==1.15.0 and still facing the same issue

File "/dbc/sc-dbc2162/abandaru/abandaru_py311/bora/install/vcsa-installer/vcsaCliInstaller/deps/cryptography/cryptography/hazmat/bindings/openssl/binding.py", line 15, in <module>
    from cryptography.exceptions import InternalError
File "/dbc/sc-dbc2162/abandaru/abandaru_py311/bora/install/vcsa-installer/vcsaCliInstaller/deps/cryptography/cryptography/exceptions.py", line 9, in <module>
    from cryptography.hazmat.bindings._rust import exceptions as rust_exceptions
ImportError: cannot import name 'exceptions' from 'cryptography.hazmat.bindings._rust' (/dbc/sc-dbc2162/abandaru/abandaru_py311/testing/vcsa-cli-installer/lin64/lib/library.zip/cryptography/hazmat/bindings/_rust/__init__.pyc)

I was using cryptography==38.0.4, python==3.10.4 and cffi==1.15.0 earlier and it was working fine If anyone has a fix for this, please do let me know

Try use cryptography==41.0.7 and cffi==1.16.0 with Python 3.11.x (mine is Python 3.11.5)

if python3.8.x try cryptography==3.4.8 and cffi==1.14.6.

hope this help. :-)

also for @techtana0x

kenz68 commented 8 months ago

@tomy128 downgrade to cryptography==3.4.8 worked for me. (python 3.9.0 and cffi 1.16.0)

tomy128 commented 8 months ago

@tomy128 downgrade to cryptography==3.4.8 worked for me. (python 3.9.0 and cffi 1.16.0)

Nice to hear.

abhinavbandaru commented 8 months ago

@tomy128 , thank you for the suggestion! I tried to downgrade to cryptography==41.0.7 and cffi==1.16.0 with python==3.11.4, I'm still facing the same issue though :/

@kenz68 thank you for the suggestion, but unfortunately I cannot downgrade to cryptography==3.4.8

abhinavbandaru commented 8 months ago

Can we can re-open this issue? I think a lot of people are still facing this issue with newer versions of cryptography and python?

reaperhulk commented 8 months ago

People facing a problem that looks like this should file a new issue with instructions about how to reproduce in a fresh virtual environment. Without that we cannot help, as these issues are almost always an environmental problem specific to the user's machine.

ascopes commented 8 months ago

@reaperhulk I think the issue here is that if we knew exactly how to reproduce this, we'd likely already know what the issue is and how to fix it if is environmental.

Without having access to more information from the library when this issue occurs, or a core developer who understands how the underlying Rust bindings work, it makes it very difficult to reproduce.

This issue was closed due to being stale, it never appeared to be actually fixed.

Given there have been 14 responses in the past three months, some with significant detail included already, it feels like it would be a waste to abandon all of this info in the hope someone re-collates it all in a new issue... otherwise this is likely never going to get resolved.

It feels like perhaps we need someone to sit and git-bisect the history to find what change caused this and work backwards from there, when they can reproduce this.

reaperhulk commented 8 months ago

People chiming in on this issue may have many different problems. Thus far every time we have looked at one of these issues it has turned out to be an environment problem and not a code issue.

For example, in your case I looked into a similar issue (https://github.com/pyca/cryptography/issues/10440) and we discovered in that case that the reporter had both a newer and older cryptography installed simultaneously, causing their problem. That's a very different environment problem than the people who appear to have some problem with their cffi install in this thread.

githubakash commented 8 months ago

I am also facing the same issue while deploying to AWS Lambda. I am using python 3.12 with fastapi = 0.99.0. Earlier I was getting the error as

"Unable to import module 'main': cannot import name 'exceptions' from 'cryptography.hazmat.bindings._rust' (unknown location)"

. after I downgrade my cryptography pkg to cryptography==3.4.8 , now I am getting the below error,

"errorMessage": "Unable to import module 'main': No module named 'cryptography.hazmat.bindings._openssl'",

I am new to python and seems, it is an rabbit-hole where we need to keep track of these dependencies which is nothing but an nightmare. has anyone got any solution to this? Also, I am not using cryptography pkg at all but it is getting installed as an dependencies to other pkg. Is there a way, I can get rid of this pkg all together??

I resolved this issue by using the Linux subsystem on windows machine ( integrated and come out-of-the-box with windows 10) to create the pkg. some how, earlier I was using powershell to create my pkg and it was showing above issues.

abhinavbandaru commented 8 months ago

Thank you guys for your inputs. I'll create a new issue for this and link it here once I do it. I'll make sure to provide as many details as I can.

abhinavbandaru commented 8 months ago

New issue here: https://github.com/pyca/cryptography/issues/10569

abdulwadood10 commented 8 months ago

I installed the paramiko package with this command

pip install --platform manylinux2014_x86_64 --target=. --implementation cp --python-version 3.9 --only-binary=:all: paramiko

zipped it with the same root folder with my lambda function. Changed the lambda function runtime to 3.9 and it worked!

spchamp commented 6 months ago

For what it's worth I was seeing this error after installing pyca cryptography on a FreeBSD 13 machine. There's no official wheel for pyca cryptography on this platform.

It seems that the rust compiler was in fact not being run during the installation from dist src with pip install. The rust compiler was not running during pip install cryptography or pip install git+https://<the_pyca_cryptograpy_git_url>. In either case, the installation with pip install produced no rust-compiled code here.

The simplest test case on this system:

 from cryptography.hazmat._oid import ObjectIdentifier

After installing via pip install, this was producing a similar error.

When checking out the pyca cryptograhy source with git, creating a virtual env in the git work tree i.e source dir, installing the source dir as editable under this virtual env, finally installing build then building from source, e.g python -m build in the virtual env under the pyca cryptography source directory, then the build runs the rust compiler and produces a usable wheel.

Could the issue be in setuptools-rust, on some platforms? Just a guess. I know that it uses some Linux-specific conditionals, while this OS is not a Linux, Mac OS, or Windows. I'm not certain exactly why it was not running the rust compiler during pip install.

I understand that GitHub doesn't support FreeBSD, NetBSD, OpenBSD, IllumOS, ... for automated builds in the workflow tooling. It might need qemu, as these platforms won't be directly available under docker on a Linux machine.

On this platform, a BSD OS, it seems that the workaround is to build a wheel directly from pyca cryptography source, then store that wheel locally and install from the locally built wheel. Perhaps this could be similar to a pyca crypotography installation with the FreeBSD port system e.g - some platform-specific build dependencies might be found there - though it produces a wheel that can be installed in any virtual env on this platform.

The installation with the wheel built from source, it works out.

werebear73 commented 6 months ago

Ran into the same issue on my Windows machine while working with fastapi and python-jose to add auth to my app. What ultimately worked for me was running: pip install --force-reinstall cryptography which pulled the correct versions of cffi for my machine which addressed the issue ModuleNotFoundError: No module named '_cffi_backend'

I did also purge my cache prior to this and updated pip: pip cache purge pip install --upgrade pip

still not sure why uninstalling and reinstalling the requirements or specific packages didnt work initially for me so doesn't feel completely resolved but force reinstall did end up working.

I tried these step and still got the "No module named '_cffi_backend'"