Skip to main content

How to Resolve Python "ModuleNotFoundError: No module named 'cryptography'"

The cryptography library is a fundamental package in Python for cryptographic operations. Encountering the ModuleNotFoundError: No module named 'cryptography' indicates that the Python interpreter cannot locate this library when your script tries to import cryptography or one of its submodules (like from cryptography.fernet import Fernet). This typically means the package isn't installed in the active Python environment or there's a configuration issue.

This guide provides a comprehensive walkthrough of the causes and step-by-step solutions to resolve this import error.

Understanding the Error: Missing Package

The ModuleNotFoundError is Python's standard signal that it searched through its defined paths (current directory, sys.path, site-packages) but could not find the module specified in the import statement. For cryptography, this almost always means the package needs to be installed or the wrong Python environment is active.

Common Causes

  • Package Not Installed: The cryptography package was never installed using pip or conda.
  • Incorrect Python Environment: The package was installed in a different Python environment (e.g., globally, another venv) than the one currently executing your script.
  • Virtual Environment Not Activated: Forgetting to activate the correct virtual environment before running the script or installing packages.
  • IDE Misconfiguration: Your IDE (VS Code, PyCharm, etc.) is configured to use a different Python interpreter than the one where cryptography resides.
  • Filename Shadowing: Naming one of your own files cryptography.py.
  • Missing Build Dependencies: Sometimes, installing cryptography requires system-level development headers (like python3-dev on Debian/Ubuntu or python3-devel on RedHat/CentOS) and a C compiler, especially if a pre-compiled binary ("wheel") isn't available for your specific OS/Python version/architecture. pip usually handles this or provides informative errors if dependencies are missing.

Solution 1: Install the cryptography Package (pip/conda)

This is the most common solution. Ensure you are in the correct environment (activate your virtual environment if using one) and run the appropriate installation command.

  • Using pip:

    pip install cryptography

    # Or more explicitly
    python -m pip install cryptography
    pip3 install cryptography
    python3 -m pip install cryptography
    py -m pip install cryptography # Windows

    # If permission issues occur (less likely in venv)
    pip install cryptography --user
    sudo pip3 install cryptography # Avoid if possible
  • Using conda (Anaconda/Miniconda):

    conda install cryptography
    # Or specify the Anaconda channel explicitly if needed
    conda install -c anaconda cryptography

Solution 2: Verify the Python Environment

Consistency is key. The environment where you install cryptography must be the same one used to run your code.

  • Checking Python Version: Use python --version / pip --version (or python3/pip3) to confirm versions match.
  • Using Virtual Environments (Recommended):
    • Create: python3 -m venv venv
    • Activate: source venv/bin/activate (Linux/macOS) or venv\Scripts\activate (Windows)
    • Install: pip install cryptography (while activated)
    • Run Script: Execute your script while the venv is active.
  • Checking IDE Interpreter Settings: Configure VS Code ("Python: Select Interpreter") or PyCharm (File > Settings > Project > Python Interpreter) to use the Python executable from within your virtual environment.

Solution 3: Check for Filename/Variable Shadowing

  • Make sure you do not have a file named cryptography.py in your project directory. Rename it if you do.
  • Avoid using cryptography as a variable name before the import.

Note on Dependencies (e.g., paramiko)

Sometimes, cryptography is not imported directly by your code but is a dependency of another library you are using (like paramiko for SSH). If you get ModuleNotFoundError: No module named 'cryptography' when importing paramiko, it usually means cryptography didn't get installed correctly as a dependency. Installing the main library (pip install paramiko) should also pull in cryptography, but if it fails, installing cryptography explicitly (Solution 1) often resolves the issue.

Debugging Steps

  • Check Installation (pip show): Activate the correct environment and run pip show cryptography. Verify it's installed and note the Location. Is this the site-packages for the interpreter your script/IDE is using?
  • Restart IDE / Kernel / Script: Close and reopen VS Code/PyCharm, restart Jupyter kernels, or stop/start your script to ensure changes are picked up.
  • Reinstall / Upgrade Package:
    pip uninstall cryptography
    pip install cryptography
    # Or upgrade
    pip install --upgrade cryptography

Platform/Tool Specific Installation Guides

These sections repeat the core pip install cryptography or conda install cryptography commands for clarity.

  • Install cryptography on Windows: Use Command Prompt or PowerShell (as Admin if needed, or use --user). Consider venv.
  • Install cryptography on macOS or Linux: Use Terminal. Use pip3. Consider venv, --user, or sudo (last resort). Note: You might need build tools (sudo apt-get install build-essential python3-dev libffi-dev libssl-dev on Debian/Ubuntu or sudo yum groupinstall "Development Tools" && sudo yum install python3-devel libffi-devel openssl-devel on RedHat/CentOS) if pip needs to compile the package.
  • Install cryptography in Visual Studio Code: Use the integrated terminal (ensure correct venv is active).
  • Install cryptography in PyCharm: Use the integrated terminal (check venv) or the UI package installer (Settings > Project > Python Interpreter > +).
  • Install cryptography in Anaconda: Activate conda environment. Use conda install cryptography (often pulls from anaconda or conda-forge channels).
  • Install cryptography in Jupyter Notebook: Best practice is to install in the terminal before starting Jupyter. Alternatively, use !pip install cryptography in a cell and restart the kernel.

Conclusion

The ModuleNotFoundError: No module named 'cryptography' indicates that Python cannot find the installed cryptography package. The most common solutions involve:

  1. Installing the package using pip install cryptography (or conda install cryptography) in the correct, active Python environment (preferably a virtual environment).
  2. Ensuring your IDE is configured to use that same Python interpreter.
  3. Verifying you haven't created a conflicting local file named cryptography.py.
  4. Installing necessary system-level build dependencies if pip fails to install a pre-compiled wheel.

By ensuring the package is correctly installed and accessible to your Python interpreter, you can resolve this common import error.