Reputation: 2154
i am trying to import a python class in another python file but i keep getting the error "TypeError: object() takes no parameters" but when written within the same file it works
i have tried the following
api.py
from flask import Flask
from flask_restful import Resource, Api
from flask_restful import reqparse
from flask import Flask, jsonify, request
app = Flask(__name__)
api = Api(app)
class CreateUser(Resource):
def post(self):
try:
parser = reqparse.RequestParser()
parser.add_argument('email', type=str, help='Email address to create user')
parser.add_argument('password', type=str, help='Password to create user')
args = parser.parse_args()
_userEmail = args['email']
_userPassword = args['password']
return {'Email': args['email'], 'Password': args['password']}
except Exception as e:
return {'error': str(e)}
main.py
from flask import Flask
import api as apii
from flask_restful import Resource, Api
CreateUser = apii.CreateUser(Resource)
app = apii.app
api = apii.api
api.add_resource(CreateUser, '/CreateUser')
if __name__ == '__main__':
app.run(debug=True)
this is the error i get
Traceback (most recent call last):
File "main.py", line 5, in <module>
CreateUser = apii.CreateUser(Resource)
TypeError: object() takes no parameters
Upvotes: 0
Views: 866
Reputation: 1332
Your class, CreateUser
takes no parameters. You only need to put its parents in parenthesis when you first create the class. When you want to create an instance of the class, you simply call the class, and in this case it takes no parameters. The following should work for you.
CreateUser = apii.CreateUser()
if the CreateUser
class had contained a def __init__(self, name)
(for e.g.) method containing an initialisation parameter name
- Then this would be reflected on instantiation of the class and would look something akin to the following:
x = CreateUser("JohnDoe")
Upvotes: 1