Skip to main content

How to Solve "ModuleNotFoundError: No module named 'jwt'" in Python

The error ModuleNotFoundError: No module named 'jwt' in Python means you're trying to use the PyJWT library (used for working with JSON Web Tokens), but it's not installed in your current Python environment.

This guide explains how to install PyJWT and how to troubleshoot common installation problems.

Understanding the Error: jwt vs. PyJWT

It's important to distinguish between the package name you use to install the library and the module name you use to import it:

  • PyJWT: This is the package name you use with pip install.
  • jwt: This is the module name you use in your Python code (e.g., import jwt).

Installing PyJWT with pip

The standard way to install PyJWT is with pip:

Basic Installation

Open your terminal (or command prompt on Windows) and run:

pip install PyJWT

If that doesn't work, try:

pip3 install PyJWT # Explicitly use pip for Python 3

Using python -m pip (If pip Isn't in Your PATH)

If the pip command fails (e.g., "command not found"), use:

python -m pip install PyJWT
python3 -m pip install PyJWT # Explicitly for Python 3
  • The python -m pip will make sure you are using the version of pip associated with your Python installation.

Windows-Specific Considerations (py launcher)

On Windows, the py launcher is often preferred:

py -m pip install PyJWT
  • py uses the most recent Python version.

Permissions Issues (sudo or --user)

On Linux/macOS, you might need sudo for system-wide installation (but virtual environments are strongly preferred):

sudo pip3 install PyJWT # System wide installation, use with caution

Or, install into your user's site-packages:

pip install PyJWT --user
  • Using the --user argument will install the package for the current user, and using sudo installs the package globally.

Always use virtual environments for Python projects. They isolate dependencies, preventing conflicts and making your projects reproducible.

Creating a Virtual Environment

python3 -m venv venv  # Create a virtual environment named "venv"
# OR: python -m venv venv
# OR: py -m venv venv (Windows)

Activating the Environment

  • macOS / Linux:

    source venv/bin/activate
  • Windows (Command Prompt):

    venv\Scripts\activate.bat
  • Windows (PowerShell):

    venv\Scripts\Activate.ps1

    If activating on Windows (PowerShell) fails, you may need to set the execution policy:

    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

After activation, your prompt will usually show the environment name (e.g., (venv)). pip install now installs into this environment only.

Installing PyJWT Inside the Environment**

With the virtual environment activated:

pip install PyJWT  # Installs into the active environment

Troubleshooting

If you're still getting ModuleNotFoundError, consider these:

  • IDE Configuration (VS Code, PyCharm): Your IDE might be using a different Python interpreter.
    • VS Code: Ctrl+Shift+P (or Cmd+Shift+P on macOS), type "Python: Select Interpreter", choose the correct one (your virtual environment).
    • PyCharm: File > Settings > Project > Python Interpreter (or PyCharm > Preferences > Project > Python Interpreter on macOS), select the correct interpreter.
  • Conflicting Installations/Multiple Python Versions: If you have multiple Python versions without virtual environments, you might have installed PyJWT for the wrong version. Always use virtual environments.
  • Incorrect Filenames/Variable Names: Never name your Python files jwt.py. This will conflict with the installed library. Similarly, don't create a variable named jwt before importing it.
  • Restarting Your IDE/Kernel: Sometimes (especially in Jupyter), you need to restart the kernel or IDE after installation.
  • Reinstalling: Sometimes uninstalling and reinstalling the package helps resolve the issue.
      pip uninstall PyJWT
    pip install PyJWT

Using PyJWT (Example)

Once installed correctly, you can use PyJWT:

import jwt

encoded = jwt.encode({"some": "payload"}, "secret", algorithm="HS256")
print(encoded)

decoded = jwt.decode(encoded, "secret", algorithms=["HS256"])
print(decoded) # Output: {'some': 'payload'}

Installing PyJWT in Specific Environments

Anaconda

conda install -c conda-forge pyjwt

Jupyter Notebook

Within a notebook cell:

!pip install PyJWT

Conclusion

The No module named 'jwt' error is solved by correctly installing the PyJWT package.

  • Always use virtual environments to isolate your project's dependencies.
  • Double-check your IDE's interpreter settings, and avoid naming conflicts.

If you follow these steps, you should be able to use PyJWT without problems.