JWT: 'module' object has no attribute 'encode' JWT: 'module' object has no attribute 'encode' python python

JWT: 'module' object has no attribute 'encode'


The problem arises if you have both JWT and PyJWT installed. When doing import jwt it is importing the library JWT as opposed to PyJWT - the latter is the one you want for encoding. I did pip uninstall JWT and pip uninstall PyJWT then finally pip install PyJWT. After that it imported the correct module and generated the token! :)


I was also facing the same issue because I had named the script from which I had been calling jwt.encode() as 'jwt.py'. So be careful while naming scripts. Try not to use any library names.


You can use the PyJWT package, where jwt.encode() works fine (no need for initialization or other kinds of stuff).