Arvind Kandaswamy
Arvind Kandaswamy

Reputation: 2231

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

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

Upvotes: 160

Views: 192667

Answers (14)

Oliver
Oliver

Reputation: 19

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.

Upvotes: 0

Adi
Adi

Reputation: 1075

This worked for me.

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

Somehow, PyJWT==2.9.0 throws an error.

Upvotes: 1

Joshua
Joshua

Reputation: 3719

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

Upvotes: 322

FaizGeeky
FaizGeeky

Reputation: 59

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

Upvotes: 2

Utku
Utku

Reputation: 449

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

Upvotes: 0

Yevgniy Shvartsman
Yevgniy Shvartsman

Reputation: 319

This worked for me:

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

Upvotes: 1

Nabat Farsi
Nabat Farsi

Reputation: 982

this worked for me:

pip install djangorestframework-jwt==1.11.0

Upvotes: 2

coderina
coderina

Reputation: 1746

In my case, I just needed to do

pip install pyjwt

Upvotes: 15

Kamil.S
Kamil.S

Reputation: 5543

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

Upvotes: 6

Nilay Singh
Nilay Singh

Reputation: 2331

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.

Upvotes: 33

Arvind Kandaswamy
Arvind Kandaswamy

Reputation: 2231

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.

Upvotes: 0

Sameh Sharaf
Sameh Sharaf

Reputation: 1131

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

Upvotes: 6

Aarya
Aarya

Reputation: 587

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.

Upvotes: 45

poxip
poxip

Reputation: 907

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

Upvotes: 14

Related Questions