Rawan J9
Rawan J9

Reputation: 221

import jwt ImportError: No module named jwt

I have been trying to run this project https://github.com/udacity/FSND-Deploy-Flask-App-to-Kubernetes-Using-EKS

I installed all the dependencies. I still did not make any adjustments. I need to run it first but I get this error when I type the command

python main.py

this is the error i get:

Traceback (most recent call last):
  File "main.py", line 8, in <module>
    import jwt
ImportError: No module named jwt

I worked with similar errors before and managed to solve them but not with this one I could not figure out the source of the problem

Upvotes: 22

Views: 96381

Answers (9)

sabrina Correa
sabrina Correa

Reputation: 1

Install: pip install python-jose

Import: from jose import JWTError, jwt

Upvotes: 0

kelin
kelin

Reputation: 12021

I had this trouble on MacOS and the provided solutions don't work for me because of this error:

× This environment is externally managed
╰─> To install Python packages system-wide, try brew install
    xyz, where xyz is the package you are trying to
    install.

    If you wish to install a Python library that isn't in Homebrew,
    use a virtual environment:

    python3 -m venv path/to/venv
    source path/to/venv/bin/activate
    python3 -m pip install xyz

    ...

So I had to install venv in order to run my script. I'm not a python guy, so it was quite challenging at first time. Here's help for people like me:

  1. Create venv folder: python3 -m venv ./venv
  2. Activate venv: source venv/bin/activate
  3. Install the required package, as Valerian answered: pip install PyJWT Note, that while you are in venv, don't need to call python3 or pip3, since python3 was already activated on step 1.
  4. Optionally, you can create "requirements.txt" here, if you have many libs, than use them as pgjones suggests in his answer.
  5. Run your script python script.py
  6. Exit venv by calling deactivate

Upvotes: 3

Amir nazary
Amir nazary

Reputation: 715

You have to have only PyJWT installed and not JWT. Make sure you uninstall JWT (pip uninstall JWT) and install PyJWT (pip install PyJWT)

Upvotes: 2

Prateek p
Prateek p

Reputation: 135

What worked for me was using import jwt instead of import PyJWT. I am using version PyJWT-2.3.0.

jwt image on vscode As you can see no errors in the above screenshot. The app runs without import errors. Image of terminal

Upvotes: 4

Filip
Filip

Reputation: 301

I have hit the same issue with pyjwt 2.1.0 which was clearly installed in my venv as well as globally. What helped was to downgrade it to version 1.7.1

pip install "PyJWT==1.7.1"

run the app and then to reinstall newest version 2.1.0

pip install "PyJWT==2.1.0"

And the issue disappeared.

Upvotes: 7

Rifat Dinc
Rifat Dinc

Reputation: 39

pip3 install flask_jwt_ex.. I was doing this without sudo. And then I was working on the program as sudo.

Upvotes: 0

Priya Varghese
Priya Varghese

Reputation: 39

Faced the same issue. Am using a guest VM running ubuntu 16.04. I have multiple versions of python installed - both 3.5 and 3.7.

After repeated tries with and without using virtualenv what worked finally is:

  1. Create a fresh virtual environment using :
    priya:~$ virtualenv -p /usr/bin/python3.7 fenv

  2. Activate the virutal environment : priya:~$ source ./fenv/bin/activate

Note : You can find the path for python3.7 by using whereis python: priya:~$ whereis python python: /usr/bin/python /usr/bin/python3.5m /usr/bin/python3.5 /usr/bin/python3.7 /usr/bin/python3.5m-config /usr/bin/python3.5-config /usr/bin/python2.7 /usr/bin/python3.7m /usr/bin/python2.7-config /usr/lib/python3.5 /usr/lib/python3.7 /usr/lib/python2.7 /etc/python /etc/python3.5 /etc/python3.7 /etc/python2.7 /usr/local/lib/python3.5 /usr/local/lib/python3.7 /usr/local/lib/python2.7 /usr/include/python3.5m /usr/include/python3.5 /usr/include/python2.7 /usr/share/python /usr/share/man/man1/python.1.gz

Referenced link is : https://stackoverflow.com/questions/1534210/use-different-python-version-with-virtualenv#:~:text=By%20default%2C%20that%20will%20be,%2Flocal%2Fbin%2Fpython3

For your project - FSWD Nanodegree - After you have activated your virtualenv, run pip install -r requirements.txt You can test by : (fenv) priya:FSND-Deploy-Flask-App-to-Kubernetes-Using-EKS :~$ python Python 3.7.9 (default, Aug 18 2020, 06:24:24) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information.

import jwt exit()

Upvotes: 0

Valerian Ardelean
Valerian Ardelean

Reputation: 406

  1. Check if PyJWTY is in the requirements file or if is installed in you system, using: pip3 install PyJWT
  2. You could also face this error if you have running on your machine two versions of python. So the correct command will be python3 main.py

Upvotes: 27

pgjones
pgjones

Reputation: 7049

This project has requirements that need to be installed for it to work. These can be installed via pip, pip install -r requirements.txt (I've linked to the requirements file in the project), which you can read more about here.

Upvotes: 2

Related Questions