简体   繁体   English

女服务员的“模块对象不可调用”

[英]“module object not callable” with Waitress

I am trying to deploy my app on Heroku.我正在尝试在 Heroku 上部署我的应用程序。 I have to use Windows, and gunicorn won't work.我必须使用 Windows,而 gunicorn 不起作用。 I tried waitress, which keeps giving me a "module not callable" error whenever I try to load any page.我试过女服务员,每当我尝试加载任何页面时,它都会给我一个“模块不可调用”的错误。

Note: I haven't deployed it on web so far, was trying heroku local before creating a publicly accessible one.注意:到目前为止,我还没有在网络上部署它,在创建一个可公开访问的heroku local之前尝试了它。 It works on localhost when using PyCharm.使用 PyCharm 时,它适用于localhost

organization of files文件的组织

/myapp
     requirements.txt
     Procfile
     /myapp
         /static
         /templates
         __init__.py

__init __.py : __init __.py :

# encoding=utf-8
import click

from myapp.application import create_app
from myapp.application import db, login_manager

app = create_app()

from myapp.config import SQLALCHEMY_TRACK_MODIFICATIONS
from myapp.models import User
from myapp.views import *

app.add_url_rule('/home', HomePage.endpoint, 
      view_func=HomePage.as_view(HomePage.endpoint), methods=['GET','POST'])
# pages are defined in views.py

#other code

if __name__ == '__main__':
    # set debug to false when moving to production
    app.run()

Procfile : Procfile

web: waitress-serve --port=5000 myapp:application

traceback:追溯:

\myapp>heroku local
[WARN] No ENV file found
14:58:51 web.1   |  ERROR:waitress:Exception when serving /home
14:58:51 web.1   |  Traceback (most recent call last):
14:58:51 web.1   |    File "c:\python34\lib\site-packages\waitress\channel.py",
line 338, in service
14:58:51 web.1   |      task.service()
14:58:51 web.1   |    File "c:\python34\lib\site-packages\waitress\task.py", lin
e 169, in service
14:58:51 web.1   |      self.execute()
14:58:51 web.1   |    File "c:\python34\lib\site-packages\waitress\task.py", lin
e 399, in execute
14:58:51 web.1   |      app_iter = self.channel.server.application(env, start_re
sponse)
14:58:51 web.1   |  TypeError: 'module' object is not callable
14:58:51 web.1   |  ERROR:waitress:Exception when serving /favicon.ico
14:58:51 web.1   |  Traceback (most recent call last):
14:58:51 web.1   |    File "c:\python34\lib\site-packages\waitress\channel.py",
line 338, in service
14:58:51 web.1   |      task.service()
14:58:51 web.1   |    File "c:\python34\lib\site-packages\waitress\task.py", lin
e 169, in service
14:58:51 web.1   |      self.execute()
14:58:51 web.1   |    File "c:\python34\lib\site-packages\waitress\task.py", lin
e 399, in execute
14:58:51 web.1   |      app_iter = self.channel.server.application(env, start_re
sponse)
14:58:51 web.1   |  TypeError: 'module' object is not callable

Any idea how to solve this?知道如何解决这个问题吗?

In your Procfile , try changing在您的Procfile ,尝试更改

web: waitress-serve --port=5000 myapp:application

to

web: waitress-serve --port=5000 myapp:app

The last argument to waitress-serve is MODULE:OBJECT , where OBJECT is the application object in MODULE . waitress-serve的最后一个参数是MODULE:OBJECT ,其中OBJECTMODULE的应用程序对象。 Here, you've named your application app :在这里,您已将应用app命名为app

app = create_app()

(You're not showing us all of your code, but it looks like myapp.application is in fact a module, not an object. You import create_app , db , and login_manager from it in your sample code.) (您没有向我们展示您的所有代码,但看起来myapp.application实际上是一个模块,而不是一个对象。您在示例代码中从中导入create_appdblogin_manager 。)

Waitress has it's own app , so you need to distinguish your app correctly. Waitress 有自己的app ,因此您需要正确区分您的app @Chris's answer shows you how to do so via Procfile . Procfile的回答向您展示了如何通过Procfile做到这Procfile Here's another way, for those using Flask:对于那些使用 Flask 的人来说,这是另一种方式:

app/ init .py :应用程序/初始化.py

from flask import Flask

app = Flask(__name__, template_folder="some path", static_folder="another path")

main.py主文件

from waitress import serve
from app import app as my_app  # renamed to distinguish from waitress' 'app'

if __name__ == "__main__":
    serve(my_app, host="localhost", port=5005)

This allows you to keep your app named app in a routes file, since that is isolated from waitress这允许您将名为app在路由文件中,因为它与女服务员隔离

app/routes.py :应用程序/routes.py

@app.route('/dothing1', methods=['POST', 'GET'])
def dothing1():
  pass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM