"模块对象不可调用"与女服务员

时间:2018-05-09 09:47:40

标签: python heroku flask server waitress

我正在尝试在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

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

2 个答案:

答案 0 :(得分: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实际上是一个模块,而不是一个对象。您导入create_appdb,以及示例代码中的login_manager。)

答案 1 :(得分:0)

女服务员拥有自己的app,因此您需要正确区分app。 @Chris的答案向您展示了如何通过Procfile做到这一点。对于使用Flask的用户,这是另一种方式:

应用程序/ 初始化 .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)

这使您可以将名为app的应用保留在路由文件中,因为它与女服务员隔离。

app / routes.py

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