pyjwt: AttributeError: module 'jwt' has no attribute 'encode'

Using import jwt

encoded = jwt.encode({'some': 'payload'}, 'secret', algorithm='HS256') Traceback (most recent call last): File “< stdin >”, line 1, in <module> AttribureError: module ‘jwt’ has no attribure ‘encode’

Same issue with using decode

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 2
  • Comments: 26 (1 by maintainers)

Most upvoted comments

use “pip install pyjwt”

Do you have a file called jwt.py in the same directory where you are trying to run the code?

You might have installed another package jwt

Did you run pip install jwt? @heroh9

Uninstalling “jwt” then installing “pyjwt” worked for me.

pip3 install pyjwt==1.5.3

did the magic

Had a similar issue. Accidentally installed jwt first. Just removing jwt and installing PyJWT did not help. Recreating my virtual environment and installing PyJWT fixed the issue.

uninstall all of them then “pip install pyjwt”

Had a similar issue. Accidentally installed jwt first. Just removing jwt and installing PyJWT did not help. Recreating my virtual environment and installing PyJWT fixed the issue.

For anyone installed jwt directly and the Python environment cannot be easily reset: Try this:

pip uninstall jwt
rm [Python Home Directory]/Lib/site-packages/jwt
pip uninstall pyjwt
pip install pyjwt

The problem is at the site-packages/jwt folder. After runs pip uninstall jwt, the folder still exists. You have to remove it manually. And then, the PyJWT folder is still stuck in site-packages. You have to reinstall it in order to move it to the default jwt folder.

pip3 install pyjwt==1.5.3

did the magic

This solved my issue. Thanks!

I had named my script “token.py” and that caused this error

pip3 install pyjwt==1.5.3 did the magic

It works, thanks. because of incopatibility djangorestframework-jwt 1.11.0 requires PyJWT<2.0.0,>=1.5.2, but you have pyjwt 2.3.0 which is incompatible

I had the same problem (but with ‘decode’). The fix was as @vungsung said, but you can fix PyJWT in one step with:

pip install --force PyJWT

pip3 install pyjwt==1.5.3

did the magic

thanks