2

I want to use PythonJWT library for working with Tokens. But I stuck at the beginning.

here's a simple Python code:

#!/usr/bin/python3
import jwt
payload={"email":"myemail@gmail.com"}
print(jwt.encode(payload))

I get the following error: (I print the last part of traceback)

Original exception was:
Traceback (most recent call last):
  File "./test_token.py", line 2, in <module>
    import jwt
  File "/usr/lib/python3/dist-packages/jwt/__init__.py", line 19, in <module>
    from .api_jwt import (
  File "/usr/lib/python3/dist-packages/jwt/api_jwt.py", line 1, in <module>
    import json
  File "/home/sinai/python/json.py", line 4, in <module>
    import urllib2
ImportError: No module named 'urllib2'

If I run the following:

sudo pip3 install urllib2

I get the error:

Collecting urllib2

ERROR: Could not find a version that satisfies the requirement urllib2 (from versions: none) ERROR: No matching distribution found for urllib2

If I run sudo pip3 install --upgrade PythonJWT I get the error:

ERROR Complete output from command /usr/bin/python3 -u -c 'import setuptools, tokenize;__file__='"'"'/tmp/pip-install-0p1cogz1/PythonJWT/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-118chehm --python-tag cp35:
  ERROR: running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib
  creating build/lib/pythonjwt
  copying pythonjwt/cli.py -> build/lib/pythonjwt
  copying pythonjwt/__init__.py -> build/lib/pythonjwt
  installing to build/bdist.linux-x86_64/wheel
  running install
  Traceback (most recent call last):
    File "<string>", line 1, in <module>
    File "/tmp/pip-install-0p1cogz1/PythonJWT/setup.py", line 47, in <module>
      'pythonjwt = pythonjwt.cli:cli',
    File "/usr/lib/python3.5/distutils/core.py", line 148, in setup
      dist.run_commands()
    File "/usr/lib/python3.5/distutils/dist.py", line 955, in run_commands
      self.run_command(cmd)
    File "/usr/lib/python3.5/distutils/dist.py", line 974, in run_command
      cmd_obj.run()
    File "/usr/lib/python3/dist-packages/wheel/bdist_wheel.py", line 215, in run
      self.run_command('install')
    File "/usr/lib/python3.5/distutils/cmd.py", line 313, in run_command
      self.distribution.run_command(command)
    File "/usr/lib/python3.5/distutils/dist.py", line 974, in run_command
      cmd_obj.run()
    File "/tmp/pip-install-0p1cogz1/PythonJWT/setup.py", line 20, in run
      raise Exception("You probably meant to install and run python-jwt")
  Exception: You probably meant to install and run python-jwt
  ----------------------------------------
  ERROR: Failed building wheel for PythonJWT
Matt Ellen
  • 11,268
  • 4
  • 68
  • 90
Sinai
  • 620
  • 1
  • 14
  • 36
  • your original question title is quite misleading as it is not really something about jwt, but just a problem loading/installing a library. – jps Jul 11 '19 at 10:12
  • Could you add the full traceback? If the error is from within `jwt`, then try updating to the newest version – FlyingTeller Jul 11 '19 at 10:16
  • `jwt.encode` doesn't use urllib2. Trying your code gives the error `AttributeError: module 'jwt' has no attribute 'encode'` – Matt Ellen Jul 11 '19 at 14:42
  • @jps, the error is strange for me too. So, I added the last part of the traceback of the error message. and of course as you can see I did not use urllib2 in my code!! – Sinai Jul 11 '19 at 15:18
  • @FlyingTeller, I cannot add full traceback because stackoverflow says you have to add more explanation. – Sinai Jul 11 '19 at 15:18
  • @Sinai: you don't use urllib2 directly, but if you read the traceback carefully you can see that jwt imports .api_jwt, which imports json, which imports urllib2. Urllib2 is not used anymore in Python3, so your jwt package is probably too old. Your attempt to upgrade failed because of the wrong name. See the last line of the error message: `Exception: You probably meant to install and run python-jwt` – jps Jul 11 '19 at 21:32
  • @jps, I know urllib2 is a dependency for jwt, but I was confused why it doest not install on my machine! You are right I did not notice to the last line of the error. I install python-jwt successfully but I still get the same error! should I import something else instead of `import jwt` ? – Sinai Jul 12 '19 at 05:01
  • I also followed the instructions on this page https://pypi.org/project/jwt/. But I get the same error: `ImportError: No module named 'urllib2'` – Sinai Jul 12 '19 at 05:17
  • 1
    What a mess! There are different JWT packages for Python. User Matt Ellen (3rd comment) obviously uses PyPi (installed with `pip install jwt`). that package has no `jwt.encode`. The package python-jwt (installed with `pip install python-jwt`) was mentioned by the OP but is not imported as `import jwt`like in the code example. The code in the question is most likely for PyJwt` (installed with `pip install pyjwt`). I just made a clean installation of Python 3.7, which contains `urllib` (see folder .\lib\urllib) already. Then I installed PyJwt and startet the code in the question. – jps Jul 12 '19 at 08:17
  • There's only one error "encode() missing 1 required positional argument: 'key'". After providing that paramter (see also the docs of PyJwt), the code is running as expected, no problems with urllib. – jps Jul 12 '19 at 08:19
  • I have python3.5 . I installed pyjwt with `pip3 install pyjwt`. and it showed `Successfully installed pyjwt-1.7.1`. I have only `import jwt` in my code, but I still get the same error!! and if I run 'pip3 list|grep -i 'jwt'' I can see `jwt , PyJWT and python-jwt`. how should I get rid of this error? in addition urllib3 is installed already by installing python3.5. – Sinai Jul 12 '19 at 10:16
  • 1
    I create a virtualenv and installed only PyJWT, everything is working fine. but without virtualenv I get error. I think there is a mess with my jwt packages on my machine. I removed them completely and installed only PyJWT but I get same error!! – Sinai Jul 12 '19 at 10:47
  • Sorry, I didn't realise there were so many jwt packages! – Matt Ellen Jul 12 '19 at 13:13
  • @MattEllen: Your edit of the title doesn't make any sense. The error message "failed building wheel" was caused by an attempt to install " pip3 install --upgrade PythonJWT", which is a package that doesn't exist. In my comment (6th comment above) I pointed out the cause.The package python-jwt has nothing to do with the original problem of the OP (9th comment). I think you should rollback the edit and retract the reopen vote (if it you voted) – jps Jul 14 '19 at 09:26
  • @jps The problem doesn't seem to be about urllib2, but about installing a library for dealing with jwt. The title reflects the question body. The comments could be turned into a useful answer. – Matt Ellen Jul 14 '19 at 09:32
  • @MattEllen The last error message caused by "pip3 install --upgrade PythonJWT" was only added by the OP during the discussion in the comments. That's not the original question. And the solution to the last error message is : "pip3 install --upgrade Python-JWT" (adding a "-" in the name. The error message even contains the solution! Result would be: closed as OT, error caused by a simple typo, not considered useful. The original problem is urllib, and the OP already said that he can overcome it with a clean installation. – jps Jul 14 '19 at 09:41
  • @jps so you agree that the question is not about urllib2 and that the comments can be turned into a useful answer? – Matt Ellen Jul 14 '19 at 22:51

0 Answers0