【问题标题】:"module object not callable" with Waitress女服务员的“模块对象不可调用”
【发布时间】:2018-05-09 09:47:40
【问题描述】:

我正在尝试在 Heroku 上部署我的应用程序。我必须使用 Windows,而 gunicorn 将无法工作。我试过女服务员,每当我尝试加载任何页面时,它都会给我一个“模块不可调用”错误。

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

文件组织

/myapp
     requirements.txt
     Procfile
     /myapp
         /static
         /templates
         __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:

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

追溯:

\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

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

【问题讨论】:

    标签: python heroku flask server waitress


    【解决方案1】:

    在你的Procfile,尝试改变

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

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

    waitress-serve 的最后一个参数是MODULE:OBJECT,其中OBJECTMODULE 中的应用程序对象。在这里,您已将应用程序命名为 app

    app = create_app()
    

    (你没有向我们展示你所有的代码,但看起来myapp.application 实际上是一个模块,而不是一个对象。你在你的示例代码。)

    【讨论】:

    • 我可以说 OBJECT 也可以是工厂方法吗?
    • @variable, "factory method" 有点加载,但是if this lines up 你应该添加--call 标志:"A number of frameworks, web.py are例如,在其应用程序对象上有工厂方法,在调用时返回可用的 WSGI 函数。对于这些情况,waitress-serve 具有 --call 标志。因此:waitress-serve --call myapp.mymodule.app.wsgi_factory 将加载 myapp.mymodule 模块,并调用 @987654341 @ 获取一个 WSGI 应用函数,传递给waitress.server".
    【解决方案2】:

    Waitress 有自己的app,所以你需要正确区分你的app。 @Chris 的回答通过Procfile 向您展示了如何做到这一点。对于那些使用 Flask 的人来说,这是另一种方式:

    app/init.py

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

    ma​​in.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)
    

    这允许您将名为 app 的应用保存在路由文件中,因为它与女服务员是隔离的

    app/routes.py

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

    【讨论】:

      猜你喜欢
      • 2019-01-04
      • 2017-06-17
      • 2012-09-28
      • 2014-09-21
      • 2022-01-09
      • 2021-12-26
      • 2011-05-30
      相关资源
      最近更新 更多