elksie5000
elksie5000

Reputation: 7782

ModuleNotFoundError: No module named 'flask_cors' on python anywhere

To state from the get go this is no criticism of pythonanywhere, but I want to run a script that used to work using the line:

from flask_cors import CORS

But I get the following error mesage:

ModuleNotFoundError: No module named 'flask_cors'

Then tried to install in my version of python:

pip3.9 install Flask-Cors

The result was:

Defaulting to user installation because normal site-packages is not writeable Looking in links: /usr/share/pip-wheels Requirement already satisfied: Flask-Cors in ./.local/lib/python3.9/site-packages (3.0.10) Requirement already satisfied: Six in /usr/local/lib/python3.9/site-packages (from Flask-Cors) (1.16.0) Requirement already satisfied: Flask>=0.9 in /usr/local/lib/python3.9/site-packages (from Flask-Cors) (2.0.0) Requirement already satisfied: Werkzeug>=2.0 in /usr/local/lib/python3.9/site-packages (from Flask>=0.9->Flask-Cors) (2.0.1) Requirement already satisfied: itsdangerous>=2.0 in /usr/local/lib/python3.9/site-packages (from Flask>=0.9->Flask-Cors) (2.0.1 ) Requirement already satisfied: click>=7.1.2 in /usr/local/lib/python3.9/site-packages (from Flask>=0.9->Flask-Cors) (7.1.2) Requirement already satisfied: Jinja2>=3.0 in /usr/local/lib/python3.9/site-packages (from Flask>=0.9->Flask-Cors) (3.0.1) Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.9/site-packages (from Jinja2>=3.0->Flask>=0.9->Flask-C ors) (2.0.1) 15:33 ~$ pip show Flask-Cors Name: Flask-Cors Version: 3.0.10 Summary: A Flask extension adding a decorator for CORS support Home-page: https://github.com/corydolphin/flask-cors Author: Cory Dolphin Author-email: corydolphin@gmail.com License: MIT Location: /home/elksie5000/.local/lib/python3.9/site-packages Requires: Six, Flask

What gives and how do I fix it?

Upvotes: -1

Views: 2504

Answers (2)

DEV_SD
DEV_SD

Reputation: 1

The ModuleNotFoundError: No module named 'flask_cors' error indicates that either:

The flask_cors package is not installed in the current Python environment.

The flask_cors package is installed but not correctly linked or recognized by the current Python environment.

Option #1:

Install flask-cors by running: pip install -U flask-cors

This code sets up a basic Flask application with Cross-Origin Resource Sharing (CORS) enabled, allowing the Content-Type header to be shared across different origins.

from flask import Flask from flask_cors import CORS

Initialize the Flask application
app = Flask(name)

Enable CORS for the application
cors = CORS(app)

Configure CORS headers
app.config['CORS_HEADERS'] = 'Content-Type'

Option #2:

Reinstall flask-cors.

Uninstall the package: pip uninstall flask-cors
Reinstall the package: pip install flask-cors

Sometimes dependency resolution can fail during the first installation attempt, especially if there are conflicting or missing dependencies. Reinstalling the package ensures that pip tries again to resolve dependencies, pulling in any updates that might resolve conflicts.

Upvotes: -1

hassan abbas
hassan abbas

Reputation: 72

I think you should try the following:

  1. Go to your Bash Console in "Consoles" menu.
  2. Open the bash control
  3. Select your virtualenv: For example my virtualenv name is flaskapp, I would wirte "workon flaskapp" in the bash console.
  4. Now type in your desired command pip3.9 install flask-cors in your case (try in lower case)
  5. Make sure you are working in the same virtualenv and the same python version

(I think you want to make an API using cors maybe)

Upvotes: -2

Related Questions