Forums

TypeError: create_app() takes 0 positional arguments but 2 were given

Hi, I'm getting - TypeError: create_app() takes 0 positional arguments but 2 were given and I can't solve this problem.

create_app is:

def create_app():
"""Application Factory"""

app = Flask(__name__, instance_relative_config=True)

app.config.from_object('config.settings')
app.config.from_pyfile('settings.py', silent=True)

middleware(app)
extensions(app)
authentication(app, User)
error_templates(app)

#Registrando Blueprints
app.register_blueprint(page)
app.register_blueprint(user)
app.register_blueprint(admin)

return app

And my wsgi.py is:

path = '/home/vitu/lilly-clientes/clientes'
if path not in sys.path:
    sys.path.append(path)
from app import create_app as application

Ah. You shouldn't pass wsgi the create_app function. Instead, if you are using create_app to create an app and return it in that function, you should give wsgi the returned app as application.

So, should it be:

from app import create_app as application

It won't also work. I get:

ImportError: cannot import name 'app'

I don't know if I understood it correctly. Sorry, I'm a noob! :)

No. you want to call yourapp = create_app(), and then from somewhere import yourapp as application

It worked! Thanks a lot!!! :-D

I have the same issue.... but the files are located in the right place (https://help.pythonanywhere.com/pages/DebuggingImportError/)

Error:

Error running WSGI application
TypeError: create_app() takes 0 positional arguments but 2 were given

file init.py (where create_app is located)

def create_app():
app = Flask(__name__)

app.config['SECRET_KEY'] = 'XXXXXX'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = 'True'

db.init_app(app)

login_manager = LoginManager()
login_manager.login_view = 'auth.login'
login_manager.init_app(app)

from .models import User

@login_manager.user_loader
 .....
return app

file wsgi.py

# +++++++++++ FLASK +++++++++++
import sys
project_home = u'/home/aemartinez/cryptosite'
if project_home not in sys.path:
    sys.path = [project_home] + sys.path
from project import create_app as application

When use the console all the files are located OK

(venv) 20:55 ~ $ python -i /var/www/aemartinez_pythonanywhere_com_wsgi.py
 >>> from project import create_app
 >>> import project                                                                                                                                       
 >>> print(project.__file__)                                                                                                                              
 /home/aemartinez/cryptosite/project/__init__.py
 >>> import sys                                                                                                                                           
 >>> print('\n'.join(sys.path))                                                                                                                           
 /home/aemartinez/cryptosite
 /var/www
 /home/aemartinez/cryptosite/venv/lib/python37.zip
 /home/aemartinez/cryptosite/venv/lib/python3.7
 /home/aemartinez/cryptosite/venv/lib/python3.7/lib-dynload
 /usr/lib/python3.7
 /home/aemartinez/cryptosite/venv/lib/python3.7/site-packages

Your function create_app is a function that returns a Flask application object, not an application object itself, so you need to call it in your WSGI file by replacing the line

from project import create_app as application

...with this:

from project import create_app
application = create_app()

Yeah!!!!! it works :D thanks!!!!

Glad to hear that!

Thanks a lot it worked for me too i had same problem thanks a lot! :D

Thanks for confirming that.

Thanks giles, it worked for me as well!!

Glad to hear that!

It worked for me too! Thanks a lot!

Glad to hear that!