PyJWT won't import jwt.algorithms (ModuleNotFoundError: No module named 'jwt.algorithms')

Question:

For some reason, PyJTW doesn’t seem to work on my virtualenv on Ubuntu 16.04, but it worked fine on my local Windows machine (inside a venv too). I’m clueless, I’ve tried different versions, copied the exact same versions as I had on my Windows machine, and yet I still couldn’t get this to work.

Installed packages:

Package                    Version
-------------------------- ---------
aiohttp                    3.6.2
async-timeout              3.0.1
attrs                      20.2.0
cachetools                 4.1.1
certifi                    2020.6.20
cffi                       1.14.3
chardet                    3.0.4
click                      7.1.2
cryptography               2.9.2
DateTime                   4.3
discord.py                 1.5.0
Flask                      1.1.2
Flask-Discord              0.1.61
flask-oidc                 1.4.0
flask-oidc2                1.5.0
httplib2                   0.18.1
idna                       2.10
itsdangerous               1.1.0
Jinja2                     2.11.2
jwt                        1.0.0
MarkupSafe                 1.1.1
multidict                  4.7.6
mysql-connector-python     8.0.21
mysql-connector-repackaged 0.3.1
oauth2client               4.1.3
oauthlib                   3.1.0
pip                        20.2.3
protobuf                   3.13.0
pyasn1                     0.4.8
pyasn1-modules             0.2.8
pycparser                  2.20
PyJWT                      1.7.1
pytz                       2020.1
requests                   2.24.0
requests-oauthlib          1.3.0
rsa                        4.6
schedule                   0.6.0
setuptools                 50.3.0
six                        1.15.0
typing-extensions          3.7.4.3
urllib3                    1.25.10
Werkzeug                   1.0.1
wheel                      0.35.1
yarl                       1.6.0
zope.interface             5.1.0

The error:

[2020-09-29 21:58:44 +0000] [2036] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/usr/lib/python3.7/site-packages/gunicorn/arbiter.py", line 583, in spaw                                                                                                                                                             n_worker
    worker.init_process()
  File "/usr/lib/python3.7/site-packages/gunicorn/workers/base.py", line 119, in                                                                                                                                                              init_process
    self.load_wsgi()
  File "/usr/lib/python3.7/site-packages/gunicorn/workers/base.py", line 144, in                                                                                                                                                              load_wsgi
    self.wsgi = self.app.wsgi()
  File "/usr/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/usr/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 49, in l                                                                                                                                                             oad
    return self.load_wsgiapp()
  File "/usr/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 39, in l                                                                                                                                                             oad_wsgiapp
    return util.import_app(self.app_uri)
  File "/usr/lib/python3.7/site-packages/gunicorn/util.py", line 358, in import_                                                                                                                                                             app
    mod = importlib.import_module(module)
  File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/soro/soros-dashboard/wsgi.py", line 1, in <module>
    from app import app
  File "/home/soro/soros-dashboard/app.py", line 9, in <module>
    import keycloak
  File "/home/soro/soros-dashboard/keycloak.py", line 4, in <module>
    from jwt.algorithms import RSAAlgorithm
ModuleNotFoundError: No module named 'jwt.algorithms'

I’m running Python 3.7.7.

Asked By: Ferry

||

Answers:

You shouldn’t have both the jwt and PyJWT packages installed, they have some namespace collisions. Try removing the jwt package and see if it works.

Answered By: vimalloc

It turned out to be an issue with my Python environment. I recreated it a few times, and eventually, it would work. I suspect that there are multiple jwt’s that were somehow installed.

Answered By: Ferry

I had the same issue. The error seems to be a conflict between the pyjwt and jwt modules (as mentioned by @vimalloc above). What worked for me was to run the following command (NOTE: I am using Python 3.6.10).

pip3 install -U pyjwt
Answered By: Danny Bullis

You don’t have to register with the ‘RSAAlgorithm’.
You have install the ‘cryptography’ too:

PyJWT==2.3.0
cryptography==36.0.1

Then you can just use the "RS256" algorithm without registration:

import jwt
jwt.encode(payload, private_key, algorithm="RS256")
Answered By: Israel

Try uninstalling jwt (pip uninstall jwt) and install python-jwt instead (pip install python-jwt).
I had another issue before where these 2 packages were incompatible with each other.

Answered By: amirhossein nazary
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.