为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

回首忆惘然 2021-09-11 15:19:08
目前我正在尝试在 PythonAnywhere 上部署我的第一个 FLASK 应用程序。我不确定这是否是正确的术语,但我有一个文件夹作为模块,因为我似乎无法找到部署我的应用程序的正确方法。我什至不确定从哪里开始解决这个问题。有什么建议吗?我的init .py 代码是:import osfrom flask import Flaskdef 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 syspath = '/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 applicationpath = '/home/ivanv257/LAMA_MAIN/'from init import app as applicationpath = '/home/ivanv257/LAMA_MAIN/'from LAMA import app as application我的源代码路径是: /home/ivanv257/LAMA_MAIN/LAMA ,虽然我也尝试过不同的组合,例如 /home/ivanv257/LAMA_MAIN/
查看完整描述

2 回答

?
慕盖茨4494581

TA贡献1850条经验 获得超11个赞

为了解决我的问题,我从 lama import 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)


查看完整回答
反对 回复 2021-09-11
  • 2 回答
  • 0 关注
  • 276 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信