TypeError: takes no arguments (2 given) when using

2019-09-10 05:29发布

问题:

I am using mod-wsgi with Apache 2.2

I have the following in WSGI script

import sys
sys.path.insert(0, '<path to my app>')

from optimization_app import optimizeInstances as application

and optimization_app.py as follows:

from flask import Flask
from flask_restful import Api
from resources.Optimization import OptimizationAlgo

def optimizeInstances():
    optimization_app = Flask(__name__)
    api = Api(optimization_app)
    api.add_resource(OptimizationAlgo, '/instances')

When I try to access the app url, I get the following in my apache error_log

TypeError: optimizeInstances() takes no arguments (2 given)

回答1:

This is occurring because you used it as the WSGI application object.

from optimization_app import optimizeInstances as application

A WSGI application is required to accept two parameters environ and start_response. So when the WSGI server is calling that function to have your application handle a request it is failing with that error.

The way you are using that function is simply wrong. Your actual WSGI application is being created inside of that function in local scope, so how would it be accessed anyway.

Would suggest you check back and look at the Flask getting started documentation.



回答2:

As I see u defined optimizeInstances() with No arguments. You might be calling optimizeInstances() function by passing 2 arguments to it.