2

我目前正在开发一个应用程序。这个网络应用程序有自己的域。最初创建时,我使用 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

管理.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
4

2 回答 2

2

PythonAnywhere 在这里开发。

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

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

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

[编辑:添加隶属关系]

于 2016-08-04T17:20:01.193 回答
0

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

# 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-12-24T15:47:40.230 回答