# 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.
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:
- Not having the
cryptography
package installed by runningpip install cryptography
. - Installing the package in a different Python version than the one you'reusing.
- Installing the package globally and not in your virtual environment.
- Your IDE running an incorrect version of Python.
- Naming your module
cryptography.py
which would shadow the official module. - 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
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
+ P
on Mac) to open the command palette.
Then type "Python select interpreter" in the field.
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.
# 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
- Install Cryptography on Windows
- Install Cryptography on macOS or Linux
- Install Cryptography in Visual Studio Code
- Install Cryptography in PyCharm
- Install Cryptography in Anaconda
- Install Cryptography in Jupyter Notebook
# Install Cryptography on Windows
To install the cryptography
module on Windows:
- Type CMD in the search bar and open the Command Prompt application.
- 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
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.
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:
- Open the root directory of your project.
- Press
Shift
and right-click in Explorer.
- Click on "Open PowerShell window here".
- 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:
- Search for "terminal" and start the application.
- Type
pip install cryptography
and press Enter.
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
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:
- Open your terminal in the root directory of your project.
- 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:
- Press CTRL + ` (Backtick) on your keyboard to open the terminal.
- Run the
pip install cryptography
command to install thecryptography
module.
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
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.
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
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:
- Press
Alt+F12
on your keyboard to open the terminal. - Run the
pip install cryptography
command to install thecryptography
module.
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
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.
- Click on "File" > "Settings" > "Project" > "Python Interpreter".
- Click on the
+
icon and typecryptography
. - Click on "Install Package".
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".
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
# Install Cryptography in Anaconda
To install cryptography in Anaconda:
- Open your Anaconda Navigator.
- Click on "Environments" and select your project.
- Type
cryptography
in the search bar to the right. - Tick the
cryptography
package and click on "Apply".
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:
- Open your terminal and type "jupyter notebook".
- Click on "New" and then "Terminal" in the browser tab.
- 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.
- Open your terminal and type "jupyter notebook".
Click on "New" and then click on "Python 3 (ipykernel)".
Type
!pip install cryptography
and click on "Run".
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
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
orpython3 -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.