python – Issue with cryptography and paramiko when building .exe via PyInstaller

Question:

There is Python 3.5.1 x64, Win 7 x64/. When I tried to run the .exe, I got an error, what can I do about it?

Traceback (most recent call last):
  File "test.py", line 83, in <module>
  File "site-packages\paramiko\transport.py", line 1073, in connect
  File "site-packages\paramiko\transport.py", line 493, in start_client
  File "site-packages\paramiko\transport.py", line 1757, in run
  File "site-packages\paramiko\kex_group1.py", line 75, in parse_next
  File "site-packages\paramiko\kex_group1.py", line 111, in _parse_kexdh_reply
  File "site-packages\paramiko\transport.py", line 1602, in _verify_key
  File "site-packages\paramiko\rsakey.py", line 58, in __init__
  File "site-packages\cryptography\hazmat\backends\__init__.py", line 35, in def
ault_backend
  File "site-packages\cryptography\hazmat\backends\multibackend.py", line 33, in
 __init__
ValueError: Multibackend cannot be initialized with no backends. If you are seei
ng this error when trying to use default_backend() please try uninstalling and r
einstalling cryptography.
Failed to execute script test

Answer:

The solution is described here https://github.com/pyinstaller/pyinstaller/issues/2013 Creating a method

def patch_crypto_be_discovery():

    """
    Monkey patches cryptography's backend detection.
    Objective: support pyinstaller freezing.
    """

    from cryptography.hazmat import backends

    try:
        from cryptography.hazmat.backends.commoncrypto.backend import backend as be_cc
    except ImportError:
        be_cc = None

    try:
        from cryptography.hazmat.backends.openssl.backend import backend as be_ossl
    except ImportError:
        be_ossl = None

    backends._available_backends_list = [
        be for be in (be_cc, be_ossl) if be is not None
    ]

and call patch_crypto_be_discovery() before working with paramiko

Scroll to Top