gpt4 book ai didi

python - 属性错误 : 'NoneType' object has no attribute '_instantiate_plugins'

转载 作者:行者123 更新时间:2023-12-05 02:07:57 26 4
gpt4 key购买 nike

我正在尝试开始使用 Flask 和 PostgreSQL 来创建一个简单的项目。为了完整起见,我将包括所有终端命令以及我的代码。在我的 SQL_Example 文件夹和带有表“flights”的本地 PostgreSQL 数据库“lecture3”中工作:

$ python3 -m venv venv
$ . venv/bin/activate
$ pip install Flask

现在我继续下面的代码:

import os

from flask import Flask, render_template, request
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

app = Flask(__name__)

engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))

@app.route("/")
def index():
flights = db.execute("SELECT * FROM flights").fetchall()
return render_template("index.html", flights=flights)

@app.route("/book", methods=["POST"])
def book():
name = request.form.get("name")
try:
flight_id = int(request.form.get("flight_id"))
except ValueError:
return render_template("error.html", message="Invalid flight number.")

if db.execute("SELECT * FROM flights WHERE id = :id", {"id": flight_id}).rowcount == 0:
return render_template("error.html", message="No such flight with that id.")
db.execute("INSERT INTO passengers (name, flight_id) VALUES (:name, :flight_id)", {"name": name, "flight_id": flight_id})
db.commit()
return render_template("success.html")

执行以下命令:

$ export FLASK_APP="application.py"
$ flask run

遇到以下错误:

Traceback (most recent call last):
File "/Users/spencermiller/Desktop/SQL_Example/venv/bin/flask", line 11, in <module>
sys.exit(main())
File "/Users/spencermiller/Desktop/SQL_Example/venv/lib/python3.7/site-packages/flask/cli.py", line 967, in main
cli.main(args=sys.argv[1:], prog_name="python -m flask" if as_module else None)
File "/Users/spencermiller/Desktop/SQL_Example/venv/lib/python3.7/site-packages/flask/cli.py", line 586, in main
return super(FlaskGroup, self).main(*args, **kwargs)
File "/Users/spencermiller/Desktop/SQL_Example/venv/lib/python3.7/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/Users/spencermiller/Desktop/SQL_Example/venv/lib/python3.7/site-packages/click/core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/Users/spencermiller/Desktop/SQL_Example/venv/lib/python3.7/site-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/Users/spencermiller/Desktop/SQL_Example/venv/lib/python3.7/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/Users/spencermiller/Desktop/SQL_Example/venv/lib/python3.7/site-packages/click/decorators.py", line 73, in new_func
return ctx.invoke(f, obj, *args, **kwargs)
File "/Users/spencermiller/Desktop/SQL_Example/venv/lib/python3.7/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/Users/spencermiller/Desktop/SQL_Example/venv/lib/python3.7/site-packages/flask/cli.py", line 848, in run_command
app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
File "/Users/spencermiller/Desktop/SQL_Example/venv/lib/python3.7/site-packages/flask/cli.py", line 305, in __init__
self._load_unlocked()
File "/Users/spencermiller/Desktop/SQL_Example/venv/lib/python3.7/site-packages/flask/cli.py", line 330, in _load_unlocked
self._app = rv = self.loader()
File "/Users/spencermiller/Desktop/SQL_Example/venv/lib/python3.7/site-packages/flask/cli.py", line 388, in load_app
app = locate_app(self, import_name, name)
File "/Users/spencermiller/Desktop/SQL_Example/venv/lib/python3.7/site-packages/flask/cli.py", line 240, in locate_app
__import__(module_name)
File "/Users/spencermiller/Desktop/SQL_Example/application.py", line 9, in <module>
engine = create_engine(os.getenv("DATABASE_URL"))
File "/Users/spencermiller/Desktop/SQL_Example/venv/lib/python3.7/site-packages/sqlalchemy/engine/__init__.py", line 479, in create_engine
return strategy.create(*args, **kwargs)
File "/Users/spencermiller/Desktop/SQL_Example/venv/lib/python3.7/site-packages/sqlalchemy/engine/strategies.py", line 56, in create
plugins = u._instantiate_plugins(kwargs)
AttributeError: 'NoneType' object has no attribute '_instantiate_plugins'

我见过一些类似的问题,但这些解决方案都对我不起作用。

环境变量:

TERM_PROGRAM=Apple_Terminal
SHELL=/bin/bash
TERM=xterm-256color
TMPDIR=/var/folders/pf/2_4xxn2s0jl7m8r0xv9wg9fr0000gp/T/
Apple_PubSub_Socket_Render=/private/tmp/com.apple.launchd.CyouqBAyRx/Render
TERM_PROGRAM_VERSION=421.1.1
OLDPWD=/Users/spencermiller
TERM_SESSION_ID=3CA9A778-D58A-405E-8EC3-D5F86D87FFFA
USER=spencermiller
SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.aXcWzrdMYX/Listeners
PATH=/Library/Frameworks/Python.framework/Versions/3.7/bin:/Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin
PWD=/Users/spencermiller/Desktop/SQL_Example
LANG=en_CA.UTF-8
XPC_FLAGS=0x0
XPC_SERVICE_NAME=0
SHLVL=1
HOME=/Users/spencermiller
LOGNAME=spencermiller
DISPLAY=/private/tmp/com.apple.launchd.WkWoFkNc6W/org.macosforge.xquartz:0
_=/usr/bin/printenv

最佳答案

在这段代码中,您尝试使用环境变量中的数据库 URL 创建 flask 引擎。

engine = create_engine(os.getenv("DATABASE_URL"))

我的猜测是你没有设置这个环境变量,所以你试图创建一个带有 None url 的引擎。

您需要设置环境变量,或将您的 DATABASE_URL 硬编码到 python 中。

db_url = 'postgresql+psycopg2://{user}:{password}@{url}/{db}'
engine = create_engine(db_url)

希望对您有所帮助。

关于python - 属性错误 : 'NoneType' object has no attribute '_instantiate_plugins' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61394584/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com