AttributeError module 'jwt' has no attribute 'encode'

avatar

Borislav Hadzhiev

Wed Apr 20 20222 min read

banner

Photo by Johnson Wang

AttributeError module 'jwt' has no attribute 'encode' #

The Python "AttributeError module 'jwt' has no attribute 'encode'" occurs when we have a local file named jwt.py and import from the PyJWT module. To solve the error, make sure you haven't got a file named jwt.py, uninstall the jwt module and install PyJWT.

attributeerror module jwt has no attribute encode

First, make sure you haven't installed the jwt module instead of PyJWT.

shell
# 👇️ uninstall jwt and PyJWT pip3 uninstall jwt pip3 uninstall PyJWT # 👇️ install PyJWT pip3 install PyJWT

Now try importing the jwt module.

main.py
import jwt encoded = jwt.encode({"some": "payload"}, "secret", algorithm="HS256") print(encoded) print(jwt.decode(encoded, "secret", algorithms=["HS256"]))

Make sure you don't have a local file named jwt.py.

If you have a local module that is named jwt.py, it would shadow the third-party PyJWT module.

Here is an example of how the error occurs in a local file named jwt.py.

jwt.py
import jwt # ⛔️ AttributeError: partially initialized module 'jwt' has no # attribute 'encode' (most likely due to a circular import). encoded = jwt.encode({"some": "payload"}, "secret", algorithm="HS256") print(encoded) print(jwt.decode(encoded, "secret", algorithms=["HS256"]))

The Python interpreter first looks for the imported module in the built-in modules, then in the current directory, then in the PYTHON PATH, then in the installation-dependent default directory.

So, when we create a local file with the same name as that of a third-party module, we effectively shadow the official module with our local file.

It doesn't have to be the file you are running directly. If you have a jwt.py file anywhere in the directory, it would shadow the official module.

You can access the __file__ property on the imported module to see whether it is shadowed by a local file.

main.py
import jwt print(jwt.__file__) # ⛔️ result if shadowed by local file # /home/borislav/Desktop/bobbyhadz_python/jwt.py # ✅ result if pulling in correct module # /home/borislav/Desktop/bobbyhadz_python/venv/lib/python3.10/site-packages/jwt/__init__.py

A good way to start debugging is to print(dir(your_module)) and see what attributes the imported module has.

jwt.py
import jwt # ✅ ['__builtins__', '__cached__', '__doc__', '__file__', # '__loader__', '__name__', '__package__', '__spec__', 'jwt'] print(dir(jwt))

If you pass a module object to the dir() function, it returns a list of names of the module's attributes.

If you try to access any attribute that is not in this list, you would get the "AttributeError: module has no attribute".

We can see that the encode method is not present in the module's attributes, which is a clear indication that we are shadowing the third-party module with our local module.

Make sure you haven't misspelled anything in your import statement as that could also cause the error.

Once you rename your file, you should be able to use the PyJWT module.

main.py
import jwt encoded = jwt.encode({"some": "payload"}, "secret", algorithm="HS256") print(encoded) print(jwt.decode(encoded, "secret", algorithms=["HS256"]))
Use the search field on my Home Page to filter through my more than 1,000 articles.