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

hun*_*ppo 5 python heroku flask waitress server

我正在尝试在 Heroku 上部署我的应用程序。我必须使用 Windows,gunicorn 不起作用。我尝试了 waitress,每当我尝试加载任何页面时,它都会给我一个“模块不可调用”错误。

注意:到目前为止,我还没有在网络上部署它,heroku local在创建一个可公开访问的版本之前正在尝试。localhost它在使用 PyCharm 时有效。

文件的组织

/myapp
     requirements.txt
     Procfile
     /myapp
         /static
         /templates
         __init__.py
Run Code Online (Sandbox Code Playgroud)

__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()
Run Code Online (Sandbox Code Playgroud)

Procfile

web: waitress-serve --port=5000 myapp:application
Run Code Online (Sandbox Code Playgroud)

追溯:

\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
Run Code Online (Sandbox Code Playgroud)

知道如何解决这个问题吗?

Chr*_*ris 4

在你的Procfile,尝试改变

web: waitress-serve --port=5000 myapp:application
Run Code Online (Sandbox Code Playgroud)

web: waitress-serve --port=5000 myapp:app
Run Code Online (Sandbox Code Playgroud)

的最后一个参数waitress-serveMODULE:OBJECTOBJECT应用程序对象在哪里MODULE。在这里,您已命名您的应用程序app

app = create_app()
Run Code Online (Sandbox Code Playgroud)

(您没有向我们展示您的所有代码,但它看起来myapp.application实际上是一个模块,而不是一个对象。您可以在示例代码中导入create_appdb、 和login_manager。)