如何基于flask megatutorial在pythonanyhwere上设置Flask

时间:2016-08-04 16:48:19

标签: python mysql flask pythonanywhere flask-migrate

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

我决定在他的书中讲述格林伯格先生的例子(在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。 (第一个没有基于教程建模并且工作得很好)
  3. 修改

    我将端口从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
    

    manage.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
    

2 个答案:

答案 0 :(得分:2)

PythonAnywhere dev here。

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

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

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

[编辑:添加了从属关系]

答案 1 :(得分: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

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