如何使用文件夹作为模块在pythonanywhere上部署Flask应用程序

时间:2018-12-08 13:14:28

标签: python python-3.x flask deployment pythonanywhere

当前,我正在尝试在PythonAnywhere上部署我的第一个FLASK应用程序。

我不确定这是否是正确的术语,但是我有一个文件夹作为模块,因此我似乎找不到正确的方法来部署应用程序。我什至不知道从哪里开始解决这个问题。有什么建议吗?

File and Folder Layout Snipped

我的 init .py代码是:

import os

from flask import Flask


def create_app(test_config=None):
    # create and configure the app
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_mapping(
        SECRET_KEY='secret',
        DATABASE=os.path.join(app.instance_path, 'LAMA.sqlite'),
    )

    if test_config is None:
        # load the instance config, if it exists, when not testing
        app.config.from_pyfile('config.py', silent=True)
    else:
        # load the test config if passed in
        app.config.from_mapping(test_config)

    # ensure the instance folder exists
    try:
        os.makedirs(app.instance_path)
    except OSError:
        pass

    # database
    from . import db
    db.init_app(app)

    # authentication blueprint
    from . import auth
    app.register_blueprint(auth.bp)


    # blog blueprint - the main index
    # from . import blog
    # app.register_blueprint(blog.bp)
    # app.add_url_rule('/', endpoint='index')

    # book blueprint 
    from . import book
    app.register_blueprint(book.bp)
    app.add_url_rule('/', endpoint='index')

    return app

我还遵循了python调试页面,在其中执行以下操作:

>>> import LAMA
>>> print(LAMA)
<module 'LAMA' from '/home/ivanv257/LAMA_MAIN/LAMA/__init__.py'>

因此在此阶段,我的WSGI配置文件中包含:

import sys

path = '/home/ivanv257/LAMA_MAIN/LAMA/__init__.py'
if path not in sys.path:
    sys.path.append(path)

from LAMA import app as application

我还尝试了许多其他组合,例如

path = '/home/ivanv257/LAMA_MAIN/LAMA/'
from init import app as application

path = '/home/ivanv257/LAMA_MAIN/'
from init import app as application

path = '/home/ivanv257/LAMA_MAIN/'
from LAMA import app as application

我的源代码路径是:/ home / ivanv257 / LAMA_MAIN / LAMA,尽管我也尝试过其他组合,例如/ home / ivanv257 / LAMA_MAIN /

错误详情:

2018-12-08 10:05:32,028: Error running WSGI application
2018-12-08 10:05:32,030: ModuleNotFoundError: No module named 'LAMA'
2018-12-08 10:05:32,030:   File "/var/www/ivanv257_pythonanywhere_com_wsgi.py", line 83, in <module>
2018-12-08 10:05:32,030:     from LAMA import app as application  # noqa

2 个答案:

答案 0 :(得分:1)

为解决我的问题,我在喇嘛导入create_app中更改了以下内容(有一些帮助):

import sys

path = '/home/ivanv257/LAMA_MAIN/LAMA'
if path not in sys.path:
    sys.path.append(path)

from lama import create_app

application = create_app()

我还必须从中删除。只是进口

    import db
    db.init_app(app)

    # authentication blueprint
    import auth
    app.register_blueprint(auth.bp)

答案 1 :(得分:0)

您很近。要部署您的应用程序,请导航到用户仪表板上的webapps页面。如果尚未这样做,请单击“添加新的Web应用程序”按钮,然后输入所需的应用程序名称。然后,在仪表板上的同一webapp页面上,向下滚动到页面的“代码”部分。单击“源代码” a href,然后将绝对路径(完整路径)添加到存储lama_main文件的init.py目录中。

接下来,单击“ WSGI配置文件”链接。在您应用程序的WSGI配置文件中,将正确的路径设置为父目录,然后从app文件中导入init.py

import sys

# add your project directory to the sys.path
project_home = u'/home/your_user_name/lama_main'
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 init import app as application #note that the module being imported from must be the file with "app" defined. 

然后,保存WSGI文件并返回到仪表板上的webapp面板。点击“重新加载{您的网站名称}”按钮。现在,您应该可以通过单击页面顶部的主链接来访问该站点。