代码之家  ›  专栏  ›  技术社区  ›  tfv

pythonanywhere中的相对导入在flask项目中不起作用

  •  0
  • tfv  · 技术社区  · 3 年前

    我正试图从一个非常有用的教程中学习

    https://www.digitalocean.com/community/tutorials/how-to-add-authentication-to-your-app-with-flask-login

    如何使用flask_login。

    我的问题是: 完整的教程在我的本地PC上运行良好,但在尝试在pythonanywhere上运行时遇到了问题 .

    这个错误似乎与相对进口无关。在最好的情况下,我想想出一些代码,既可以在我的机器上本地运行,也可以在pythonanywhere上运行。

    举个简单的例子,项目结构如下:

    └── flask_auth_app
        └── project
            ├── __init__.py       
            ├── auth.py           
            ├── main.py        
    

    与文件

    __init__.py

    from flask import Flask
    from flask_sqlalchemy import SQLAlchemy
    
    # init SQLAlchemy so we can use it later in our models
    db = SQLAlchemy()
    
    def create_app():
        app = Flask(__name__)
    
        app.config['SECRET_KEY'] = 'secret-key-goes-here'
        app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
    
        db.init_app(app)
    
        # blueprint for auth routes in our app
        from .auth import auth as auth_blueprint
        app.register_blueprint(auth_blueprint)
    
        # blueprint for non-auth parts of app
        from .main import main as main_blueprint
        app.register_blueprint(main_blueprint)
    
        return app 
    

    auth.py

    from flask import Blueprint
    from . import db
    
    auth = Blueprint('auth', __name__)
    
    @auth.route('/login')
    def login():
        return 'Login'
    
    @auth.route('/signup')
    def signup():
        return 'Signup'
    
    @auth.route('/logout')
    def logout():
        return 'Logout'    
    

    main.py

    from flask import Blueprint
    from . import db
    
    main = Blueprint('main', __name__)
    
    @main.route('/')
    def index():
        return 'Index'
    
    @main.route('/profile')
    def profile():
        return 'Profile' 
    

    为了在本地PC上执行此操作,我创建了一个环境变量

    set FLASK_APP=project
    

    并从flask_auth_app目录运行flask

    flask run
    

    一切都很顺利。

    我现在已经将文件传输到pythonanywhere,并在那里使用以下配置:

    source code: /home/grammaster/flask_auth_app
    working directory: /home/grammaster/flask_auth_app
    

    WSGI配置文件

    # 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 = '/home/grammaster/flask_auth_app/project'
    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 project import app as application  # noqa
    

    由此产生的错误消息为

    2021-08-13 05:58:26,704: Error running WSGI application
    2021-08-13 05:58:26,706: ImportError: cannot import name 'app' from 'project' (/home/grammaster/flask_auth_app/./project/__init__.py)
    2021-08-13 05:58:26,706:   File "/var/www/grammaster_pythonanywhere_com_wsgi.py", line 16, in <module>
    2021-08-13 05:58:26,707:     from project import app as application  # noqa
    

    如果我将设置更改为

    source code: /home/grammaster/flask_auth_app/project
    working directory: /home/grammaster/flask_auth_app/project
    

    和使用

    from main import app as application  # noqa
    

    由此产生的误差是

    2021-08-13 06:10:53,086: Error running WSGI application
    2021-08-13 06:10:53,089: ImportError: attempted relative import with no known parent package
    2021-08-13 06:10:53,089:   File "/var/www/grammaster_pythonanywhere_com_wsgi.py", line 16, in <module>
    2021-08-13 06:10:53,089:     from main import app as application  # noqa
    2021-08-13 06:10:53,089: 
    2021-08-13 06:10:53,089:   File "/home/grammaster/flask_auth_app/project/main.py", line 2, in <module>
    2021-08-13 06:10:53,089:     from . import db
    

    我确信这个问题相当微不足道,但我似乎对pythonanywhere上的相对导入和一般的flask服务器缺乏一些基本的了解。

    0 回复  |  直到 3 年前
        1
  •  2
  •   tfv    3 年前

    我终于找到了让上述示例在pythonanywhere上使用相对导入的解决方案。问题源于WSGI配置文件中的错误路径。

    对于上述示例和路径 正确的WSGI配置文件

    # 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 = '/home/grammaster/flask_auth_app'
    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 project import create_app
    application = create_app()