How to fix JWT no attribute encode? I have a Raspberry pi 4 and am working on a project about zoom api. I had this error and simply fix the problem by running this command:
pip3 install pyjwt==1.5.3
But recently I moved the project to an Ubuntu server and this fix can’t solve the error. I tried to fix it using a different approach. This is what I’m using:
- Ubuntu 22
- Python 3.10
- Python virtual environment
Let’s start from here:
cd
mkdir zoom
cd zoom
nano myscript.py
My script contains:
token = jwt.encode({'iss': API_KEY, 'exp': int(time() + 5000)}, API_SEC, algorithm='HS256').decode("utf-8")
Python virtual environment
sudo apt-get install python3-venv
python3 -m venv ./
source ./bin/activate
In case you have installed jwt module, this is the uninstall command
(zoom)$ pip3 uninstall jwt
(zoom)$ pip3 uninstall pyjwt
Install pyjwt
(zoom)$ pip3 install pyjwt==1.5.3
Run the script:
(zoom)$ python3 myscript.py
#error received
#ImportError: cannot import name 'Mapping' from 'collections' (/usr/lib/python3.10/collections/__init__.py)
If you received this error, let’s see how to fix this:
(zoom)$ python3
>> from collections import Mapping
Fixing error : cannot import name ‘Mapping’ from ‘collections’
If you recevied error: ImportError: cannot import name ‘Mapping’ from ‘collections’ (/usr/lib/python3.10/collections/__init__.py), let’s try this with collections.abc
>> from collections.abc import Mapping
If you recevied no error, then we are good to go
(zoom)$ python3 myscript.py
#File "/home/user/zoom/lib/python3.10/site-packages/jwt/api_jwt.py", line 5, in <module>
from collections import Mapping
Edit
nano /home/user/zoom/lib/python3.10/site-packages/jwt/api_jwt.py
Find from collections import Mapping, change it to and save it.
from collections.abc import Mapping
Run your script again. If you received this error:
from .api_jws import PyJWS
File "/home/user/zoom/lib/python3.10/site-packages/jwt/api_jws.py", line 5, in <module>
from collections import Mapping
Edit api_jws.py
(zoom)$ nano /home/user/zoom/lib/python3.10/site-packages/jwt/api_jws.py
Change to this:
from collections.abc import Mapping
I hope this fix works for you. To run your script outside virtual environment, do this will inside your the environment:
(zoom)$ which python3
#/home/user/zoom/bin/python3
Edit your script:
nano myscript.py
from
#!/bin/python3
. . .
token = jwt.encode({'iss': API_KEY, 'exp': int(time() + 5000)}, API_SEC, algorithm='HS256').decode("utf-8")
to this:
#!bin/python3
. . .
token = jwt.encode({'iss': API_KEY, 'exp': int(time() + 5000)}, API_SEC, algorithm='HS256').decode("utf-8")
Exit environment:
deactivate
chmod +x myscript.py
Run your script:
./myscript.py