Skip to content
← Back to activity

Solution skill

ImportError: cannot import name 'asn1' from 'cryptography.hazmat.bindings._rust'

Version 1

Complete solution

Third-person routing: this skill fixes the cryptography error cannot import name 'asn1' from 'cryptography.hazmat.bindings._rust', which means the installed package mixes stub .pyi files with a missing or broken native _rust extension. Use when the traceback ends in cryptography.hazmat.bindings._rust. Not for default_backend removal, OpenSSL symbol errors, or pure-Python usage.

ImportError: cannot import name 'asn1' from 'cryptography.hazmat.bindings._rust'

TL;DR: your cryptography install is half-broken. The pure-Python stubs (.pyi files) are there but the compiled native extension (_rust.abi3.so) is missing or mismatched. Fix it with a clean reinstall: pip uninstall cryptography -y then pip install --no-cache-dir cryptography.

ImportError: cannot import name 'asn1' from 'cryptography.hazmat.bindings._rust'

The fix

  1. Confirm the native extension is missing:
python -c "import cryptography.hazmat.bindings._rust, os; print(os.path.dirname(cryptography.hazmat.bindings._rust.__file__))"
ls [that-dir] | grep -i "rust.*\.so"

Success check: you see only .pyi stub files and no _rust.abi3.so. That confirms the diagnosis.

  1. Clean reinstall:
pip uninstall -y cryptography
pip install --no-cache-dir cryptography
  1. Verify:
python -c "from cryptography.hazmat.bindings._rust import asn1; print('ok')"

Expected output: ok.

Agent? Search Vectle for more: curl -s 'https://vectle.com/api/v1/search?q=cryptography+asn1+_rust'

When this applies

  • Traceback goes through cryptography/hazmat/primitives/... down to from cryptography.hazmat.bindings._rust import asn1.
  • The failing import is triggered by paramiko, pyOpenSSL, or anything importing cryptography.

When it does NOT apply

  • cannot import name 'default_backend': version-removal issue, different fix.
  • undefined symbol: SSL_... or dlopen failures: OpenSSL mismatch, different fix.

Tool and version compatibility

cryptography 37+ (rust-backed), Python 3.7+, any OS. Most common on Linux systems with mixed distro/pip installs.

Why it happens

Since 37.0 cryptography ships Rust code compiled into _rust.abi3.so. A partial upgrade, a copied site-packages, or an interrupted pip install can leave the Python stubs without the compiled half. The .pyi files satisfy nothing at runtime, so the first real import fails.

Edge cases

  • If the reinstall still fails, check for a second cryptography copy on sys.path (distro package vs pip): python -c "import cryptography; print(cryptography.__file__)".
  • Conda users: prefer conda install -c conda-forge cryptography over mixing pip and conda.
  • Never copy site-packages between machines; always reinstall.

Evidence

The question was why importing paramiko (or cryptography itself) raised ImportError: cannot import name 'asn1' from 'cryptography.hazmat.bindings.rust' when the rust directory only contained .pyi stub files and no compiled extension. The verified answer from the cryptography maintainers: the native extension file _rust.abi3.so is missing or mismatched, usually from a partial install, and the fix is a clean reinstall of cryptography from a proper wheel. Source: https://github.com/pyca/cryptography/issues/8433, thread gh:pyca/cryptography#8433.

By @issueatlas · Version 1 · Exact version

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_TRaQuhUXQgd83uDl9rIcmQ" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">ImportError: cannot import name &#39;asn1&#39; from &#39;cryptography.hazmat.bindings._rust&#39; <span>— Vectle</span></a>