If you encounter AttributeError module ‘jwt’ has no attribute ‘encode’ in Python, keep reading our article. Today, we will explain to you the root of the problem and give you some solutions to fix this problem.
Reason of the error “AttributeError module ‘jwt’ has no attribute ‘encode'” in Python
PyJWT is a library that helps you decode and encode Json Web Token to add security tokens to your project. JWT is a Python module used to generate and verify JSON Web Tokens.
When you work with the PyJWT, you must declare it by the command import jwt. Unfortunately, if your system also installed JWT, the command will understand that you import the JWT and give you the error.
Example:
import jwt # Declare a secure variable secure_val = jwt.encode({"Learn Share IT": "Have a good day"}, "secret", algorithm="HS256") # Print the encoded information print(secure_val) # Decode the variable to get the original information print(jwt.decode(secure_val, "secret", algorithms=["HS256"]))
Result:
Traceback (most recent call last):
line 4, in <module>
secure_val = jwt.encode({"Learn Share IT": "Have a good day"}, "secret", algorithm="HS256")
AttributeError: module 'jwt' has no attribute 'encode'
Also, you will get the same error if there is a Python file named jwt.pt in the same folder as that:

Solutions for this error
Uninstall the JWT package
When you install both packages, there is a conflict between them. Therefore, you must uninstall the JWT package to eliminate the error and run your programs without raising errors. However, you also need to refresh your system, so you also need to uninstall the PyJWT package and then reinstall it.
To uninstall the JWT package, use the following command in the terminal:
pip uninstall jwt pip uninstall pyjwt pip install pyjwt
After executing the commands above, rerun your program to see the result:
Code:
import jwt # Declare a secure variable secure_val = jwt.encode({"Learn Share IT": "Have a good day"}, "secret", algorithm="HS256") # Print the encoded information print("The encoded information is:\n" + secure_val) # Decode the variable to get the original information print("The original information is:") print(jwt.decode(secure_val, "secret", algorithms=["HS256"]))
Result:
The encoded information is:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJMZWFybiBTaGFyZSBJVCI6IkhhdmUgYSBnb29kIGRheSJ9.gGp4H-3_NB_0jUbx4_9Po_kG2bpd_6rId6glJa12K3A
The original information is:
{'Learn Share IT': 'Have a good day'}
Rename the duplicate file’s name
Another reason for this error is that there is a file named jwt.py in the same folder. So, you must rename this file to make the error disappear. Change the file’s name to another, and you will overcome the error.

Summary
In summary, the AttributeError module ‘jwt’ has no attribute ‘encode’ in Python occurs because of the conflict between the PyJWT and JWT module or the duplicate file’s name. Uninstall the JWT module and change the file’s name to get rid of the error.
Maybe you are interested:
- AttributeError: ‘str’ object has no attribute ‘write’
- AttributeError: ‘float’ object has no attribute ’round’
- AttributeError: ‘list’ object has no attribute ‘get’

My name is Robert Collier. I graduated in IT at HUST university. My interest is learning programming languages; my strengths are Python, C, C++, and Machine Learning/Deep Learning/NLP. I will share all the knowledge I have through my articles. Hope you like them.
Name of the university: HUST
Major: IT
Programming Languages: Python, C, C++, Machine Learning/Deep Learning/NLP