8

I'm following the flask-cors tutorial from the documentation here: https://pypi.python.org/pypi/Flask-Cors

but when i installed it on my raspberry pi and run my python app i'm getting this error

Traceback (most recent call last): File "app.py", line 3, in <module> from flask_cors import CORS, cross_origin ImportError: No module named 'flask_cors'

here is my python script:

from flask import Flask
from Main import main
from flask_cors import CORS, cross_origin    
app = Flask(__name__)
CORS(app)
main = main() 

@app.route('/turn' ,methods=['GET', 'OPTIONS'])
def index():
  return main.turn()

if __name__ == '__main__': 
  app.run(debug=True, host='0.0.0.0')
Nelson Candia
  • 179
  • 1
  • 2
  • 14

8 Answers8

8

If you import sys and print(sys.path), this will show you where your available packages are installed.

In the event that pip installed flask_cors outside of one of these directories, you should move the file to one of the directories or you can sys.path.append(<your path to flask_cors>).

To prevent pip from installing into a bad directory, I would recommend this answer

JacobIRR
  • 6,814
  • 6
  • 30
  • 50
  • 2
    So yeah basically the problem in raspberry pi is that when you're installing a package with pip there's a difference with pip for python 2 and pip3 for python 3. So the easiest way to install flask-cors on the raspberry for python 3 is with `pip3 install -U flask-cors` and not `pip install -U flask-cors`. it's working perfect now. – Nelson Candia Feb 09 '18 at 22:48
7

It worked for me finally

pip install -U flask-cors
desertnaut
  • 46,107
  • 19
  • 109
  • 140
2

I spent a day trying to resolve this. Here are my steps: First i closed my VSCODE and open it again, then run

pip uninstall flask
pip uninstall flask_cors
pip3 install flask
pipenv shell
pipenv install flask_cors
pip install pipreqs
git add .
git commit -am "New cor resolved"
git push heroku master
Olu Ayeni
  • 109
  • 2
  • 6
1

I had a similar issue where after installing flask_cors it was still giving me a ModuleNotFoundError. Try this:

sudo pip3 uninstall flask_cors
sudo pip3 install Flask-Cors

It's a subtle difference but it worked for me. Even though flask_cors appears to install a package and it's written that way in python when actually importing it, the actual package name installed with pip is Flask-Cors.

1

In case you encounter this trouble in Conda env, ap per conda documentation, use-

conda install -c conda-forge flask_cors
0

Following command working in my case. Change python version according to your system.

python3.7 -m pip install flask
Sameera K
  • 614
  • 11
  • 15
0

If you're using pipenv, you might just need to turn on your virtual environment too by typing:

pipenv shell

while in your root directory (with the pipfile.lock file in it).

-1

If you are running your python script as sudo, then...

sudo pip install -U flask-cors
desertnaut
  • 46,107
  • 19
  • 109
  • 140
Good4Nothing
  • 21
  • 1
  • 3