9
votes

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')
8
$ pip install -U flask-cors and check that it installs in the correct directoryJacobIRR
i already did that is not workingNelson Candia
i mean, it says is already installed but is not workingNelson Candia
if you print(sys.path), you have to ensure that flask_cors is inside one of those printed directories.JacobIRR
NameError: name 'sys' is not definedNelson Candia

8 Answers

8
votes

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

8
votes

It worked for me finally

pip install -U flask-cors
2
votes

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
1
votes

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
votes

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

conda install -c conda-forge flask_cors
0
votes

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

python3.7 -m pip install flask
0
votes

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
votes

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

sudo pip install -U flask-cors