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')

1 Answers1

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.

Foivoschr
  • 435
  • 1
  • 3
  • 14