ModuleNotFoundError: No module named 'cryptography' - Python | bobbyhadz (2024)

# ModuleNotFoundError: No module named 'cryptography' - Python

The Python "ModuleNotFoundError: No module named 'cryptography'" occurs whenwe forget to install the cryptography module before importing it or install itin an incorrect environment.

To solve the error, install the module by running thepip install cryptography command.

ModuleNotFoundError: No module named 'cryptography' - Python | bobbyhadz (1)

Open your terminal in your project's root directory and install thecryptography module.

shell

Copied!

# πŸ‘‡οΈ in a virtual environment or using Python 2pip install cryptography# πŸ‘‡οΈ for python 3 (could also be pip3.10 depending on your version)pip3 install cryptography# πŸ‘‡οΈ if you get permissions errorsudo pip3 install cryptographypip install cryptography --user# πŸ‘‡οΈ if you don't have pip in your PATH environment variablepython -m pip install cryptography# πŸ‘‡οΈ for python 3 (could also be pip3.10 depending on your version)python3 -m pip install cryptography# πŸ‘‡οΈ using py alias (Windows)py -m pip install cryptography# πŸ‘‡οΈ for Anacondaconda install -c anaconda cryptography# πŸ‘‡οΈ for Jupyter Notebook!pip install cryptography

If you also use the paramiko package, makesure to install it as well.

shell

Copied!

# πŸ‘‡οΈ in a virtual environment or using Python 2pip install paramiko# πŸ‘‡οΈ for python 3 (could also be pip3.10 depending on your version)pip3 install paramiko# πŸ‘‡οΈ for Anacondaconda install -c anaconda paramiko

After you install the cryptographypackage, try importing it as follows.

main.py

Copied!

from cryptography.fernet import Fernetkey = Fernet.generate_key()f = Fernet(key)token = f.encrypt(b'example 123')print(token)

# Common causes of the error

The error occurs for multiple reasons:

  1. Not having the cryptography package installed by runningpip install cryptography.
  2. Installing the package in a different Python version than the one you'reusing.
  3. Installing the package globally and not in your virtual environment.
  4. Your IDE running an incorrect version of Python.
  5. Naming your module cryptography.py which would shadow the official module.
  6. Declaring a variable named cryptography which would shadow the importedvariable.

If the error persists, get your Python version and make sure you are installingthe package using the correct Python version.

shell

Copied!

python --version

ModuleNotFoundError: No module named 'cryptography' - Python | bobbyhadz (2)

For example, my Python version is 3.10.4, so I would install thecryptography package with pip3.10 install cryptography.

shell

Copied!

pip3.10 install cryptography# πŸ‘‡οΈ if you get permissions error use pip3 (NOT pip3.X)sudo pip3 install cryptography

Notice that the version number corresponds to the version of pip I'm using.

If the PATH for pip is not set up on your machine, replace pip withpython3 -m pip:

shell

Copied!

# πŸ‘‡οΈ make sure to use your version of Python, e.g. 3.10python3 -m pip install cryptography

If the "No module named 'cryptography'" error persists, try restarting your IDEand development server/script.

# Check if the package is installed

You cancheck if you have the cryptography package installedby running the pip show cryptography command.

shell

Copied!

# πŸ‘‡οΈ check if you have cryptography installedpip show cryptography# πŸ‘‡οΈ if you don't have pip set up in PATHpython -m pip show cryptography

The pip show cryptography command will either state that the package is notinstalled or show a bunch of information about the package, including thelocation where the package is installed.

# Make sure your IDE is using the correct Python version

If the package is not installed, make sure your IDEis using the correct version of Python.

If you have multiple Python versions installed on your machine, you might have installed the cryptography package using the incorrect version or your IDE might be set up to use a different version.

For example, In VSCode, you can press CTRL + Shift + P or (⌘ + Shift + Pon Mac) to open the command palette.

Then type "Python select interpreter" in the field.

ModuleNotFoundError: No module named 'cryptography' - Python | bobbyhadz (3)

Then select the correct python version from the dropdown menu.

ModuleNotFoundError: No module named 'cryptography' - Python | bobbyhadz (4)

Your IDE should be using the same version of Python (including the virtual environment) that you are using to install packages from your terminal.

# Install the package in a Virtual Environment

If you are using a virtual environment, make sure you are installingcryptography in your virtual environment and not globally.

You can try creating a virtual environment if you don't already have one.

shell

Copied!

# πŸ‘‡οΈ use correct version of Python when creating VENVpython3 -m venv venv# πŸ‘‡οΈ activate on Unix or MacOSsource venv/bin/activate# πŸ‘‡οΈ activate on Windows (cmd.exe)venv\Scripts\activate.bat# πŸ‘‡οΈ activate on Windows (PowerShell)venv\Scripts\Activate.ps1# πŸ‘‡οΈ install cryptography in virtual environmentpip install cryptography

If the python3 -m venv venv command doesn't work, try the following 2commands:

  • python -m venv venv
  • py -m venv venv

Your virtual environment will use the version of Python that was used to createit.

If the error persists, make sure you haven't named a module in your project as cryptography.py because that would shadow the original cryptography module.

You also shouldn't be declaring a variable named cryptography as that wouldalso shadow the original module.

# Try reinstalling the package

If the error is not resolved, try to uninstall the cryptography package andthen install it.

shell

Copied!

# πŸ‘‡οΈ check if you have cryptography installedpip show cryptography# πŸ‘‡οΈ if you don't have pip set up in PATHpython -m pip show cryptography# πŸ‘‡οΈ uninstall cryptographypip uninstall cryptography# πŸ‘‡οΈ if you don't have pip set up in PATHpython -m pip uninstall cryptography# πŸ‘‡οΈ install cryptographypip install cryptography# πŸ‘‡οΈ if you don't have pip set up in PATHpython -m pip install cryptography

Try restarting your IDE and development server/script.

You can also try to upgrade the version of the cryptography package.

shell

Copied!

pip install cryptography --upgrade# πŸ‘‡οΈ if you don't have pip set up in PATHpython -m pip install cryptography --upgrade

If the error persists, follow the operating system-specific instructions on how to install cryptography.

# Table of Contents

  1. Install Cryptography on Windows
  2. Install Cryptography on macOS or Linux
  3. Install Cryptography in Visual Studio Code
  4. Install Cryptography in PyCharm
  5. Install Cryptography in Anaconda
  6. Install Cryptography in Jupyter Notebook

# Install Cryptography on Windows

To install the cryptography module on Windows:

  1. Type CMD in the search bar and open the Command Prompt application.
  2. Type pip install cryptography and press Enter.

cmd

Copied!

pip install cryptography# πŸ‘‡οΈ for Python 3pip3 install cryptography# πŸ‘‡οΈ if you don't have pip in your PATH environment variablepython -m pip install cryptography# πŸ‘‡οΈ for Python 3python3 -m pip install cryptography# πŸ‘‡οΈ using py aliaspy -m pip install cryptography# πŸ‘‡οΈ if you get permissions errorpip install cryptography --user# πŸ‘‡οΈ for Anacondaconda install -c anaconda cryptography

ModuleNotFoundError: No module named 'cryptography' - Python | bobbyhadz (5)

If the command doesn't succeed, try running CMD as an administrator.

Right-click on the search result, click on "Run as administrator" and run the pip install command.

ModuleNotFoundError: No module named 'cryptography' - Python | bobbyhadz (6)

If you get the error'pip' is not recognized as an internal or external command,use the python -m command when installing cryptography.

shell

Copied!

python -m pip install cryptographypython3 -m pip install cryptographypy -m pip install cryptography

If you also use the paramiko package, makesure to install it as well.

shell

Copied!

pip install paramikopip3 install paramikopython -m pip install paramikopython3 -m pip install paramiko# πŸ‘‡οΈ for Anacondaconda install -c anaconda paramiko

Alternatively, you can install the cryptography module in a virtualenvironment:

  1. Open the root directory of your project.
  2. Press Shift and right-click in Explorer.

ModuleNotFoundError: No module named 'cryptography' - Python | bobbyhadz (7)

  1. Click on "Open PowerShell window here".
  2. Run the following commands.

PowerShell

Copied!

# πŸ‘‡οΈ might also be: "python3 -m venv venv"python -m venv venv# πŸ‘‡οΈ activate on Windows (PowerShell)venv\Scripts\Activate.ps1# πŸ‘‡οΈ activate on Windows (cmd.exe)venv\Scripts\activate.bat# πŸ‘‡οΈ install cryptography in virtual environmentpip install cryptography

If the python -m venv venv command doesn't work, try the following 2 commands:

  • python3 -m venv venv
  • py -m venv venv.

If you see an error message thatps1 cannot be loaded because running scripts is disabled on this system,run the following command, type "yes" when prompted and rerun the activationcommand.

PowerShell

Copied!

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

You can verify that the cryptography module is installed by using the pip show cryptography command.

PowerShell

Copied!

pip show cryptographypip3 show cryptographypython -m pip show cryptographypython3 -m pip show cryptography

The pip show cryptography command will either state that the package is notinstalled or show a bunch of information about the package, including thelocation where the package is installed.

# Install Cryptography on macOS or Linux

To install cryptography on macOS or Linux:

  1. Search for "terminal" and start the application.
  2. Type pip install cryptography and press Enter.

ModuleNotFoundError: No module named 'cryptography' - Python | bobbyhadz (8)

terminal

Copied!

pip install cryptography# πŸ‘‡οΈ for Python 3pip3 install cryptography# πŸ‘‡οΈ if you get permissions errorsudo pip3 install cryptography# πŸ‘‡οΈ if you don't have pip in your PATH environment variablepython -m pip install cryptography# πŸ‘‡οΈ for python 3python3 -m pip install cryptography# πŸ‘‡οΈ alternative if you get permissions errorpip install cryptography --user# πŸ‘‡οΈ for Anacondaconda install -c anaconda cryptography

ModuleNotFoundError: No module named 'cryptography' - Python | bobbyhadz (9)

If you get an error that pip isn't found, use the python -m command.

terminal

Copied!

python -m pip install cryptographypython3 -m pip install cryptography

If you get a permissions error, prefix the command with sudo.

terminal

Copied!

sudo pip install cryptographysudo pip3 install cryptography

If you also use the paramiko package, makesure to install it as well.

shell

Copied!

pip install paramikopip3 install paramikopython -m pip install paramikopython3 -m pip install paramiko# πŸ‘‡οΈ for Anacondaconda install -c anaconda paramiko

Alternatively, you can install the cryptography package in a virtualenvironment:

  1. Open your terminal in the root directory of your project.
  2. Run the following commands.

shell

Copied!

# πŸ‘‡οΈ could also be "python -m venv venv"python3 -m venv venv# πŸ‘‡οΈ activate virtual env on macOS or Linuxsource venv/bin/activate# πŸ‘‡οΈ install cryptography in virtual environmentpip install cryptography

Your virtual environment will use the version of Python that was used to createit.

If the python3 -m venv venv command doesn't work, use python -m venv venv instead.

You can use the pip show command to verifycryptography has been installedsuccessfully.

shell

Copied!

pip show cryptographypip3 show cryptographypython -m pip show cryptographypython3 -m pip show cryptography

The pip show cryptography command will either state that the package is notinstalled or show a bunch of information about the package.

# Install cryptography in Visual Studio Code

To install cryptography in Visual Studio Code:

  1. Press CTRL + ` (Backtick) on your keyboard to open the terminal.
  2. Run the pip install cryptography command to install the cryptographymodule.

terminal

Copied!

pip install cryptography# πŸ‘‡οΈ for Python 3pip3 install cryptography# πŸ‘‡οΈ if you get permissions errorsudo pip3 install cryptography# πŸ‘‡οΈ if you don't have pip in your PATH environment variablepython -m pip install cryptography# πŸ‘‡οΈ for python 3python3 -m pip install cryptography# πŸ‘‡οΈ using py aliaspy -m pip install cryptography# πŸ‘‡οΈ alternative if you get permissions errorpip install cryptography --user

ModuleNotFoundError: No module named 'cryptography' - Python | bobbyhadz (10)

You can also open the terminal in Visual studio code by pressing CTRL+Shift+P and then typing "View: Toggle Terminal".

When installing Python modules in Visual Studio code, make sure thatyour IDE is configured to use the correct Python version.

Press CTRL+Shift+P or (⌘ + Shift + P on Mac) to open the commandpalette.

Then type "Python select interpreter" in the field.

ModuleNotFoundError: No module named 'cryptography' - Python | bobbyhadz (11)

Then select the correct Python version from the dropdown menu.

ModuleNotFoundError: No module named 'cryptography' - Python | bobbyhadz (12)

Your IDE should be using the same version of Python (including the virtual environment) that you are using to install packages from your terminal.

You can use the python --version command if you need to get your version ofPython.

terminal

Copied!

python --versionpython3 --version

ModuleNotFoundError: No module named 'cryptography' - Python | bobbyhadz (13)

If you also use the paramiko package, make sure to install it as well.

shell

Copied!

pip install paramikopip3 install paramikopython -m pip install paramikopython3 -m pip install paramiko# πŸ‘‡οΈ for Anacondaconda install -c anaconda paramiko

You can also try creating a virtual environment if you don't already have one.

terminal

Copied!

# πŸ‘‡οΈ could also be "python -m venv venv" or "py -m venv venv"python3 -m venv venv# πŸ‘‡οΈ activate on Unix or MacOSsource venv/bin/activate# πŸ‘‡οΈ activate on Windows (cmd.exe)venv\Scripts\activate.bat# πŸ‘‡οΈ activate on Windows (PowerShell)venv\Scripts\Activate.ps1# πŸ‘‡οΈ install cryptography in virtual environmentpip install cryptography

Your virtual environment will use the version of Python that was used to createit.

# Install cryptography in PyCharm

To install cryptography in PyCharm:

  1. Press Alt+F12 on your keyboard to open the terminal.
  2. Run the pip install cryptography command to install the cryptographymodule.

terminal

Copied!

pip install cryptography# πŸ‘‡οΈ for Python 3pip3 install cryptography# πŸ‘‡οΈ if you get permissions errorsudo pip3 install cryptography# πŸ‘‡οΈ if you don't have pip in your PATH environment variablepython -m pip install cryptography# πŸ‘‡οΈ for python 3python3 -m pip install cryptography# πŸ‘‡οΈ using py aliaspy -m pip install cryptography# πŸ‘‡οΈ alternative if you get permissions errorpip install cryptography --user

ModuleNotFoundError: No module named 'cryptography' - Python | bobbyhadz (14)

If you also use the paramiko package, makesure to install it as well.

shell

Copied!

pip install paramikopip3 install paramikopython -m pip install paramikopython3 -m pip install paramiko# πŸ‘‡οΈ for Anacondaconda install -c anaconda paramiko

Alternatively, you can use the IDE itself to install the module.

  1. Click on "File" > "Settings" > "Project" > "Python Interpreter".
  2. Click on the + icon and type cryptography.
  3. Click on "Install Package".

ModuleNotFoundError: No module named 'cryptography' - Python | bobbyhadz (15)

When installing Python modules in PyCharm, make sure that your IDE is configured to use the correct version of Python.

Click on "File" > "Settings" > "Project" > "Python Interpreter".

ModuleNotFoundError: No module named 'cryptography' - Python | bobbyhadz (16)

Then select the correct Python version from the dropdown menu.

Your IDE should be using the same version of Python (including the virtual environment) that you are using to install packages from your terminal.

You can use the python --version command if you need to get your version ofPython.

terminal

Copied!

python --versionpython3 --version

ModuleNotFoundError: No module named 'cryptography' - Python | bobbyhadz (17)

# Install Cryptography in Anaconda

To install cryptography in Anaconda:

  1. Open your Anaconda Navigator.
  2. Click on "Environments" and select your project.
  3. Type cryptography in the search bar to the right.
  4. Tick the cryptography package and click on "Apply".

ModuleNotFoundError: No module named 'cryptography' - Python | bobbyhadz (18)

Alternatively, you can install the cryptography package with a command.

If you are on Windows, search for "Anaconda Prompt" and open theapplication.

If you are on macOS or Linux, open your terminal.

Run the following command to install the cryptography package.

shell

Copied!

# πŸ‘‡οΈ using condaconda install -c anaconda cryptography# πŸ‘‡οΈ Alternatively use `pip`pip install cryptography# πŸ‘‡οΈ for Python 3pip3 install cryptography# πŸ‘‡οΈ if you get permissions errorsudo pip3 install cryptography# πŸ‘‡οΈ if you don't have pip in your PATH environment variablepython -m pip install cryptography# πŸ‘‡οΈ for python 3python3 -m pip install cryptography# πŸ‘‡οΈ using py aliaspy -m pip install cryptography# πŸ‘‡οΈ alternative if you get permissions errorpip install cryptography --user

If you also use the paramiko package, makesure to install it as well.

shell

Copied!

pip install paramikopip3 install paramikopython -m pip install paramikopython3 -m pip install paramiko# πŸ‘‡οΈ for Anacondaconda install -c anaconda paramiko

Click on thefollowing articleif you need to install a specific version of the package using Anaconda.

# Install cryptography in Jupyter Notebook

To install cryptography in Jupyter Notebook:

  1. Open your terminal and type "jupyter notebook".

ModuleNotFoundError: No module named 'cryptography' - Python | bobbyhadz (19)

  1. Click on "New" and then "Terminal" in the browser tab.

ModuleNotFoundError: No module named 'cryptography' - Python | bobbyhadz (20)

  1. Type pip install cryptography and press Enter.

shell

Copied!

# πŸ‘‡οΈ using pippip install cryptography# πŸ‘‡οΈ for Python 3pip3 install cryptography# πŸ‘‡οΈ if you get permissions errorsudo pip3 install cryptography# πŸ‘‡οΈ if you don't have pip in your PATH environment variablepython -m pip install cryptography# πŸ‘‡οΈ for python 3python3 -m pip install cryptography# πŸ‘‡οΈ using py aliaspy -m pip install cryptography# πŸ‘‡οΈ using condaconda install -c anaconda cryptography# πŸ‘‡οΈ alternative if you get permissions errorpip install cryptography --user

Alternatively, you can use the Python ipykernel.

  1. Open your terminal and type "jupyter notebook".

ModuleNotFoundError: No module named 'cryptography' - Python | bobbyhadz (21)

  1. Click on "New" and then click on "Python 3 (ipykernel)".ModuleNotFoundError: No module named 'cryptography' - Python | bobbyhadz (22)

  2. Type !pip install cryptography and click on "Run".

ModuleNotFoundError: No module named 'cryptography' - Python | bobbyhadz (23)

Note that the pip install command must be prefixed with an exclamation mark ifyou use this approach.

shell

Copied!

!pip install cryptography

Once you type the command, click "Run" to install the cryptography module.

If you get a permissions error, e.g. "[WinError: 5] Access is denied", add the--user option to the installation command.

shell

Copied!

!pip install cryptography --user

ModuleNotFoundError: No module named 'cryptography' - Python | bobbyhadz (24)

If the error persists, try torestart the Jupyter Kerneland rerun the command.

If you also use the paramiko package, makesure to install it as well.

shell

Copied!

pip install paramikopip3 install paramikopython -m pip install paramikopython3 -m pip install paramiko# πŸ‘‡οΈ for Anacondaconda install -c anaconda paramiko

I'm an expert in Python development with a focus on module management and troubleshooting. My extensive experience allows me to provide detailed insights into resolving issues like the "ModuleNotFoundError: No module named 'cryptography'" in Python. Let's break down the concepts and information provided in the article:

1. Introduction

The error occurs when the cryptography module is not installed or is installed in the wrong environment.

2. Installing the cryptography Module

To solve the error, the article suggests using the pip install cryptography command. It provides various commands for different environments:

  • In a virtual environment or using Python 2: pip install cryptography
  • For Python 3: pip3 install cryptography or python3 -m pip install cryptography
  • Using aliases or in specific environments like Anaconda and Jupyter Notebook.

3. Additional Steps for paramiko Package

If using the paramiko package, it should also be installed using commands like pip install paramiko.

4. Common Causes of the Error

The article lists possible reasons for the error, such as not having the cryptography package installed, incorrect Python versions, global installation, IDE configuration issues, and potential naming conflicts.

5. Verifying Installation

The user is advised to check if the cryptography package is installed using the pip show cryptography command.

6. Python Version Considerations

It emphasizes using the correct Python version and provides commands to check the Python version (python --version) and install packages accordingly.

7. Restarting IDE and Development Server

If the error persists, restarting the IDE and development server/script is suggested.

8. Table of Contents

The article provides a structured table of contents, making it easy for users to navigate to specific sections based on their needs.

9. Install Instructions for Different Environments

Detailed installation instructions are provided for Windows, macOS, Linux, Visual Studio Code, PyCharm, Anaconda, and Jupyter Notebook.

10. Troubleshooting and Upgrading

Steps for troubleshooting, uninstalling/reinstalling the package, and upgrading the cryptography package are outlined.

11. Environment-Specific Instructions

Specific instructions are given for different environments, including virtual environments, Visual Studio Code, PyCharm, Anaconda, and Jupyter Notebook.

This comprehensive guide covers a wide range of scenarios, ensuring users have the information they need to resolve the ModuleNotFoundError related to the cryptography module in Python.

ModuleNotFoundError: No module named 'cryptography' - Python | bobbyhadz (2024)
Top Articles
Latest Posts
Article information

Author: Saturnina Altenwerth DVM

Last Updated:

Views: 5690

Rating: 4.3 / 5 (64 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Saturnina Altenwerth DVM

Birthday: 1992-08-21

Address: Apt. 237 662 Haag Mills, East Verenaport, MO 57071-5493

Phone: +331850833384

Job: District Real-Estate Architect

Hobby: Skateboarding, Taxidermy, Air sports, Painting, Knife making, Letterboxing, Inline skating

Introduction: My name is Saturnina Altenwerth DVM, I am a witty, perfect, combative, beautiful, determined, fancy, determined person who loves writing and wants to share my knowledge and understanding with you.