【发布时间】:2021-10-16 10:06:36
【问题描述】:
我正在尝试从一个非常有用的教程中学习
关于如何使用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 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 和一般烧瓶服务器上的相对导入缺乏一些基本的了解。
【问题讨论】:
-
您在哪里创建了 app 变量?我在代码中没有看到。
-
app 变量在
__init__.py中创建。我知道 create_app 函数在启动烧瓶服务器时会以某种方式自动执行。 -
也许本教程将帮助您充分理解相对导入以使它们工作napuzba.com/a/import-error-relative-no-parent,否则,可能只是切换到使用完全导入,因为它们更容易理解
-
@Glenn 这个教程绝对有帮助,谢谢。我认为同时我已经理解了相对导入部分,但是,剩余的问题似乎源于pythonanywhere调用flask的方式,假设我还没有完全理解某种配置。上面列出的具有相对导入的packacke在我的本地PC上运行,问题只是对pythonanywhere的适应,很可能将SET FLASK_APP命令转移到pythonanywhere可以理解的配置
-
一点都不好,但我现在已经把所有东西都放在一个文件里了,这样就可以了。
标签: python flask pythonanywhere