ImportError: cannot import name 'Fernet' from partially initialized module 'cryptography.fernet'
Explains why `from cryptography.fernet import Fernet` throws ImportError: cannot import name 'Fernet' from partially initialized module 'cryptography.fernet' and how to fix it by renaming a local file called cryptography.py. Use when your own script or module shadows the installed cryptography package. Not for version-related Fernet import problems.
ImportError: cannot import name 'Fernet' from partially initialized module 'cryptography.fernet'
TL;DR: you have a file named cryptography.py (or a folder named cryptography) in your working directory, and Python imports it instead of the real package. Rename your file to something else, delete any cryptography.pyc or pycache leftovers, and the import works again.
ImportError: cannot import name 'Fernet' from partially initialized module 'cryptography.fernet' (most likely due to a circular import)The fix
- Find the shadowing file:
ls cryptography.py
python -c "import cryptography; print(cryptography.__file__)"Success check: the printed path points at site-packages, not your project folder. If it points at your folder, that file is the problem.
- Rename your file to anything else, for example crypto_helpers.py, and update any imports of it.
- Clear stale bytecode:
rm -f cryptography.pyc
rm -rf __pycache__- Verify:
python -c "from cryptography.fernet import Fernet; print('ok')"Expected output: ok.
Agent? Search Vectle for more: curl -s 'https://vectle.com/api/v1/search?q=cryptography+fernet+partially+initialized'
When this applies
- The phrase "partially initialized module" appears in the error.
import cryptographyresolves to a file inside your own project.- The same code works when run from a different directory.
When it does NOT apply
- The error is
No module named 'cryptography', which means it is not installed at all. - The error names a missing name like default_backend, which is a version problem, not shadowing.
Why this happens
Python puts the script's directory first on sys.path. A local cryptography.py wins over the installed package, so cryptography.fernet resolves inside your half-imported file, which has no Fernet in it yet. This is a circular import of your own making, and renaming the file is the only real fix. Prefixing local module names (my_crypto.py) avoids it permanently.
Compatibility
Any Python version, any cryptography version. The trigger is the filename, not the library.
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.