167

I am getting a Module not found error when using jwt. Here is how I declared it:

def create_jwt_token():
    payload = {
        "iat": int(time.time())
    }

    shared_key = REST_API_TOKEN
    payload['email'] = EMAIL
    payload['password'] = PASSWORD

    jwt_string = jwt.encode(payload, shared_key)
    encoded_jwt = urllib.quote_plus(jwt_string)  # URL encode the JWT string

    return encoded_jwt

The error message says encode is not found in jwt. I did a tab on jwt and found that the encode is a method inside jwt.JWT. I tried changing it to

jwt_string = jwt.JWT.encode(payload, shared_key)

and it gives this error:

unbound method encode() must be called with JWT instance as first argument (got dict instance instead)

What am I doing it wrong? Here is the version information of my Python environment:

2.7.10 |Anaconda 2.3.0 (64-bit)| (default, May 28 2015, 16:44:52) [MSC v.1500 64 bit (AMD64)]

2
  • You need to initialize the JWT object. Does jwt.JWT().encode(...) work? Commented Oct 18, 2015 at 13:43
  • jwt.JWT().encode(...) doesnt work. also how do i initialize the JWT object? Commented Oct 18, 2015 at 15:19

16 Answers 16

341

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! :)

Sign up to request clarification or add additional context in comments.

8 Comments

yup, that was the case. there was a package named plain "jwt" installed by mistake which was screwing up with "PyJWT" 's JWT module.
Presently both jwt and pyjwt are reporting the same versions from pip install, 10.0.1. I had to come here to double check that I really wanted pyjwt
Make sure the flask-jwt module is also uninstalled. One more thing the pip install PyJWT=1.6.4 makes it work perfectly for me. pip install PyJWT=1.6.4
for me it was PyJWT version github.com/jpadilla/pyjwt/blob/master/… had to dowgrade from version 2
In the past I've gotten both of these installed, and corrected it in the way described. However today I've seen this when I didn't have JWT installed. I needed to uninstall and re-install PyJWT. I got rid of 2.6.0, and then installed the latest (2.8.0) and now it's working. Would love to know why.
|
50

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.

2 Comments

very important and fixed my problem
Thats it!!! Thanks!!! Don't name your local files like library files!
35

I solved this problem and @josua's answer is correct I would like to answer with details. In my case, pyJwt was already installed. I was using getream client

And then I was trying to install jwt using: jwt package

And it is a known issue Issue Related to JWT

So the actual problem is a quote from Yoshida:

Unfortunately, no. As of now both libraries use the same jwt module namespace and Python's module system cannot resolve import jwt deterministically.

So I checked my pip freeze and jwt was installed and I fixed this issue by using these commands:

pip uninstall jwt==1.0.0
pip uninstall PyJWT
pip install PyJWT

And now my code:

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

works fine.

3 Comments

I got the following error "attributeerror module 'jwt' has no attribute 'get unverified header' ". Then i removed jwt and installed PyJWT and it started working fine.
How to implement this fix when using a requirements.txt file to install packages on a server? Without using manual commands?
On macOS Monterey, this answer was the solution. Uninstall both jwt and PyJWT first and then install PyJWT.
15

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

Comments

15

In my case, I just needed to do

pip install pyjwt

Comments

6

Use PyJWT instead. I faced the same issue with jwt so I uninstalled it and used PyJWT instead.

Comments

6

Apart from (re)installing the PyJWT dependency through pip which other answers already mention make sure following files are not in the current directory (i.e. pwd) you're either running python in or your .py script:

jwt.py
token.py

2 Comments

This is actually the real reason (apparently) after renaming the file name, all worked like a charm. THANK YOU
For me it was token.py. How silly...
2

this worked for me:

pip install djangorestframework-jwt==1.11.0

Comments

2

Simply replace the module you are using. You might have used jwt instead of pyJWT

pip uninstall jwt
and install module pyJWT

pip install pyJWT

It should work.

:) Happy Coding

1 Comment

If this doesn't work Uninstall both pip uninstall jwt pip uninstall PyJWT and thn try pip install PyJWT works for me
2

This worked for me.

pip uninstall jwt
pip uninstall PyJWT
pip install PyJWT==2.8.0

Somehow, PyJWT==2.9.0 throws an error.

Comments

1

This worked for me:

pip uninstall Flask-JWT && pip install Flask-JWT

Comments

1

Other possible source of error I have seen: Executing the statements from interactive python works, however, when trying to run as a script, the same circular import error can happen if the script file was named token.py, i.e.,

python token.py

will cause the import to fail. Rename your custom module.

1 Comment

naming your script jwt.py also causes this problem, thank you for the hint about token.py
0

After trying several workarounds, I created a new Python notebook with the same code and it appears to be working. I am not sure what was the issue before.

1 Comment

Does your notebook use a different virtual environment? The libraries may be different in that environment.
0

For my case, I uninstalled jwt and pyjwt and then reinstalled pyjwt with the latest version. However, it was not a solution for me. According to @John Hanley's answer, this worked on MacOS. I have Windows 11 Home.

After uninstalling both jwt and pyjwt, installing the pyjwt with a specific version worked as @Sayan Biswas mentioned.

After I run the command below, the problem was fixed.

pip install PyJWT==1.6.4

Comments

0

In my case, the issue was that pip wasn’t using the same Python environment as my script.
Even though PyJWT was installed, Python couldn’t find it (No module named jwt).

What worked for me was running:

python3 -m pip install PyJWT

This ensures that the package is installed using the exact pip linked to your current Python interpreter (virtual environment or system-wide).

After that, the import jwt worked fine !

Comments

-1

just by python 3.11

python3
Python 3.11.4 (main, Jul  5 2023, 08:41:25) [Clang 14.0.6 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import jwt
>>> encoded_jwt = jwt.encode({"some": "payload"}, "secret", algorithm="HS256")
>>> jwt.decode(encoded_jwt, "secret", algorithms=["HS256"])
{'some': 'payload'}

install nothing use

(.venv) (base) oscc:aiagent cc$ pip uninstall JWT
WARNING: Skipping JWT as it is not installed.
(.venv) (base) oscc:aiagent cc$ pip uninstall PyJWT
Found existing installation: PyJWT 2.10.1
Uninstalling PyJWT-2.10.1:
  Would remove:
    /Users/cc/Downloads/java_stu/aiagent/.venv/lib/python3.9/site-packages/PyJWT-2.10.1.dist-info/*
    /Users/cc/Downloads/java_stu/aiagent/.venv/lib/python3.9/site-packages/jwt/*
Proceed (Y/n)? y
  Successfully uninstalled PyJWT-2.10.1
(.venv) (base) oscc:aiagent cc$ pip install PyJWT=1.6.4
ERROR: Invalid requirement: 'PyJWT=1.6.4': Expected end or semicolon (after name and no valid version specifier)
    PyJWT=1.6.4
         ^
Hint: = is not a valid operator. Did you mean == ?
(.venv) (base) oscc:aiagent cc$ pip install pyjwt
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting pyjwt
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/61/ad/689f02752eeec26aed679477e80e632ef1b682313be70793d798c1d5fc8f/PyJWT-2.10.1-py3-none-any.whl (22 kB)
Installing collected packages: pyjwt
Successfully installed pyjwt-2.10.1

error allways

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

no use

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[19], line 2
      1 import jwt
----> 2 encoded_jwt = jwt.encode({"some": "payload"}, "secret", algorithm="HS256")

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





        

1 Comment

This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.