2

I have made an rest api on python with flask(port:5000), and i am making a get and a post request from a web site (port:80). I am getting the cors error, so i tried to create a response header in my rest api for the site. But i am getting the import error:

Traceback (most recent call last):
File "C:\Users\arist\Desktop\Aristo-api.py", line 3, in <module>
from flask_cors import CORS
ModuleNotFoundError: No module named 'flask_cors'

I have downloaded the flask cors module and upgrade it and made sure that is in the right path but it is still not working.

API code:

from flask import Flask, jsonify, request, Response
import json
from flask_cors import CORS  #error here

app = Flask(__name__)
CORS(app)
table_num = 0;
orders= []
put_bill = []

@app.route('/order', methods=['PUT'])
def submitorder():
    request_order = request.get_json()
    orders.append(request_order)
    response = Response("successfully submitted order", status=200)
    return response

@app.route('/all', methods=['GET'])
def get_all_orders():
   return jsonify(orders)

@app.route('/order/<int:num>', methods=['GET'])    
def get_by_tablenum(num):
   for table in orders:
       if table['tablenum']==num:
           return jsonify(table)
    return Response('invalid table number', status=404)

 @app.route('/menu', methods = ['GET'])
    def get_menu():
        menu_txt = open("C:\\Users\\arist\\Desktop\\New_Menu\\Menu.txt", "r")
        menu_fin = menu_txt.read()
        response = menu_fin
        return response

  @app.route('/bill/<string:tableid>', methods=['PUT'])
   def ask_for_bill(tableid):
       put_bill.append(tableid)
       return Response("table successfully asked for the bill", status=200)

  @app.route('/bill/all', methods=['GET'])
    def get_all_bills():
       return jsonify(put_bill)



  app.run(port=5000, host='0.0.0.0')
7
  • Does this solve your problem? stackoverflow.com/questions/48714769/… Commented Apr 27, 2020 at 14:03
  • Do you use any kind of virtual environment? Commented Apr 27, 2020 at 14:06
  • @Anwarvic I use the default idle from python 3.8 32-bit windows 10 Commented Apr 27, 2020 at 18:28
  • Did you use pip or pip3? Commented Apr 27, 2020 at 18:43
  • @Anwarvic Both, the module is proper installed Commented Apr 27, 2020 at 21:10

1 Answer 1

1

You first need to make sure that the flask-cors module that you installed is compatible with your version of python (32bits or 64bits). If you are still getting the error after checking the version then, as a last resort, you can try finding the python files assosiated with flask_cors (including the file itself) and copying them to your project files. If it still doesn't work you can try uninstalling the previous packets using: "pip unistall flask_cors" and reinstalling them.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.