【问题标题】:How to set up Flask on pythonanyhwere based on flask megatutorial如何在 pythonanywhere 上基于烧瓶巨型教程设置 Flask
【发布时间】:2016-08-04 16:48:18
【问题描述】:

我目前正在开发一个应用程序。此网络应用程序有其自己的域。最初创建时,我使用 cname 设置域和注册商,并在几个小时后成功显示 “这是一个烧瓶应用程序...” 类似的东西。

我决定效仿 Grinberg 先生在他的书中的例子(在 localhost 上功能齐全)。所以我将我的个人存储库克隆到 pythonanywhere 并运行以下命令。

python manage.py db init
python manage.py db upgrade
python manage.py migrate

到目前为止一切正常。我使用 mysql workbench 检查了 mysql 数据库。

现在我的问题来了。

当我运行python manage.py runserver

它抛出了以下错误。

/home/username/.virtualenvs/webapp/local/lib/python2.7/site-packages

/flask_sqlalchemy/__init__.py:800: UserWarning: SQLALCHEMY_TRACK_MODIFICA
TIONS adds significant overhead and will be disabled by default in the future.  Set it to True to suppress this warning.
  warnings.warn('SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True to su
ppress this warning.')
Traceback (most recent call last):
  File "manage.py", line 20, in <module>
    manager.run()
  File "/home/username/.virtualenvs/webapp/local/lib/python2.7/site-packages/flask_script/__init__.py", line 412, in run
    result = self.handle(sys.argv[0], sys.argv[1:])
  File "/home/username/.virtualenvs/webapp/local/lib/python2.7/site-packages/flask_script/__init__.py", line 383, in handle
    res = handle(*args, **config)
  File "/home/username/.virtualenvs/webapp/local/lib/python2.7/site-packages/flask_script/commands.py", line 425, in __call__
    **self.server_options)
  File "/home/username/.virtualenvs/webapp/local/lib/python2.7/site-packages/flask/app.py", line 843, in run
    run_simple(host, port, self, **options)
  File "/home/username/.virtualenvs/webapp/local/lib/python2.7/site-packages/werkzeug/serving.py", line 677, in run_simple
    s.bind((hostname, port))
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 98] Address already in use

我尝试禁用 wsgi.py 文件(将所有内容注释掉)还是一样。

须知:

  1. 我有一个付费帐户。
  2. 这是 pythonanywhere 上的第二个 webapp。 (第一个不是根据教程建模的,效果很好)

编辑

我将端口从 5000 更改为 9000。它在控制台中运行。但我不能访问我的网站。我应该注释掉 wsgi 文件吗?

目前看起来像这样:

import sys

# # add your project directory to the sys.path
project_home = u'/home/username/e_orders/e_orders'
if project_home not in sys.path:
    sys.path = [project_home] + sys.path

# # import flask app but need to call it "application" for WSGI to work
from manager import app as application

ma​​nage.py

import os
from app import create_app, db
from app.models import User
from flask_script import Manager, Shell, Server
from flask_migrate import Migrate, MigrateCommand


app = create_app(os.getenv('FLASK_CONFIG') or 'default')
manager = Manager(app)
migrate = Migrate(app, db)


def make_shell_context():
    return dict(app=app, db=db, User=User)

manager.add_command('shell', Shell(make_context=make_shell_context))
manager.add_command('db', MigrateCommand)
manager.add_command('runserver', Server(port=9000))
if __name__ == '__main__':
    manager.run()

编辑 2

上面的 wsgi 配置出现以下错误。

错误日志

ImportError: No module named manager
2016-08-04 17:42:39,589 :Error running WSGI application
Traceback (most recent call last):
  File "/bin/user_wsgi_wrapper.py", line 154, in __call__
    app_iterator = self.app(environ, start_response)
  File "/bin/user_wsgi_wrapper.py", line 170, in import_error_application
    raise e
ImportError: No module named manager

【问题讨论】:

  • 您正在使用哪个端口运行。它说这个端口已经被使用了。
  • 我发现默认是port=5000。我会修改它并回复你。
  • 好的,如果它适合你,使用 ctrl+c 否则它会产生同样的错误
  • wsgi 呢?
  • 如果您使用 http:://hostanme:portnum/... 访问它,这意味着您处于调试模式。使用 wsgi,您可以像 http:://hostname/.... 一样使用它

标签: python mysql flask pythonanywhere flask-migrate


【解决方案1】:

PythonAnywhere 在这里开发。

如果您从 PythonAnywhere 上的控制台运行 Flask 应用程序,则实际上无法从其他任何地方访问它。它可以很好地运行,但没有任何请求可以路由到它。所以没有必要从控制台运行任何东西(除非你只是在测试语法错误,我猜)。

相反,您需要在“Web”选项卡上创建一个 Web 应用程序——看起来您已经这样做了。然后使用您似乎发现的 WSGI 文件进行路由。

如果您已完成所有这些操作,那么当您访问出现在“Web”标签上的域时(通常类似于 yourusername.pythonanywhere.com),您应该会看到您的网站。如果您遇到错误,请查看错误日志(也从“Web”选项卡链接),这应该可以帮助您进行调试。

[编辑:添加隶属关系]

【讨论】:

  • 所以我重新加载了它。关闭了 bash 运行服务器连接。更改了注册商上的 cname。但它仍然不起作用。 wsgi 文件对您来说是否正确配置?我是否从 manager.py 调用正确的应用程序
  • 如果我可以查看您的帐户,调试此问题可能会更容易。请在 support@pythonanywhere.com 上给我们留言,我们可以从那里获取它,并可能在此处发布任何普遍有用的结果。
  • 我修复了它,我会尽快发布我的答案。谢谢大家的见解!
【解决方案2】:

抱歉,耽搁了这么久。 运行服务器的解决方案如下。

# This file contains the WSGI configuration required to serve up your
# web application at http://<your-username>.pythonanywhere.com/
# It works by setting the variable 'application' to a WSGI handler of some
# description.
#
# The below has been auto-generated for your Flask project

import sys

# # add your project directory to the sys.path
project_home = u'/home/username/mysite/'
if project_home not in sys.path:
    sys.path = [project_home] + sys.path

# # import flask app but need to call it "application" for WSGI to work
from manage import app as application

我也会发布数据库设置...

【讨论】:

    猜你喜欢
    • 2016-04-23
    • 2018-10-09
    • 2016-03-30
    • 1970-01-01
    • 2014-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-19
    相关资源
    最近更新 更多