121

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)]

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Arvind Kandaswamy
  • 1,821
  • 3
  • 21
  • 30

12 Answers12

251

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

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
Joshua
  • 2,979
  • 1
  • 14
  • 20
  • 5
    yup, that was the case. there was a package named plain "jwt" installed by mistake which was screwing up with "PyJWT" 's JWT module. – Iman Akbari Feb 28 '16 at 12:32
  • 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` – John May 09 '18 at 07:15
  • 3
    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` – Sayan Biswas Sep 10 '19 at 06:04
  • for me it was PyJWT version https://github.com/jpadilla/pyjwt/blob/master/CHANGELOG.md#jwtencode-return-type had to dowgrade from version 2 – Amir Heshmati Jan 09 '21 at 20:00
  • 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. – Keeely Aug 11 '23 at 09:30
31

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.

Aarya
  • 447
  • 5
  • 5
25

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.

Nilay Singh
  • 2,201
  • 6
  • 31
  • 61
  • 1
    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. – Jnana Mar 23 '21 at 06:34
  • How to implement this fix when using a requirements.txt file to install packages on a server? Without using manual commands? – s2t2 Jun 29 '22 at 12:37
  • On macOS Monterey, this answer was the solution. Uninstall both jwt and PyJWT first and then install PyJWT. – John Hanley Nov 19 '22 at 11:25
15

In my case, I just needed to do

pip install pyjwt
coderina
  • 1,583
  • 13
  • 22
14

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

poxip
  • 909
  • 8
  • 25
6

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

Sameh Sharaf
  • 1,103
  • 1
  • 13
  • 15
3

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
Kamil.S
  • 5,205
  • 2
  • 22
  • 51
2

this worked for me:

pip install djangorestframework-jwt==1.11.0
Nabat Farsi
  • 840
  • 1
  • 9
  • 17
1

This worked for me:

pip uninstall Flask-JWT && pip install Flask-JWT
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Arvind Kandaswamy
  • 1,821
  • 3
  • 21
  • 30
  • Does your notebook use a different virtual environment? The libraries may be different in that environment. – Raja Feb 06 '21 at 17:44
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

Utku
  • 419
  • 2
  • 12
  • 19
0

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

FaizGeeky
  • 39
  • 3
  • If this doesn't work Uninstall both pip uninstall jwt pip uninstall PyJWT and thn try pip install PyJWT works for me – FaizGeeky Aug 03 '23 at 09:28