python - Python Flask 应用程序导入问题访问 db 类方法
问题描述
我有一个简单的 Flask 应用程序工厂,我正在尝试使用 cx_oracle 在我的路线中访问 oracle 数据库。
simple_app[DIR]
|
+--- __init__.py[FILE]
|
+--- routes[DIR]
| |
| +--- __init__.py[FILE]
| +--- route_1.py[FILE]
| +--- route_2.py[FILE]
| +--- ...
| +--- route_n.py[FILE]
|
+--- lib[DIR]
|
+--- __init__.py[FILE]
+--- oracle.py[FILE]
我在 simple_app/lib/oracle.py 中写了一个类:
import cx_Oracle
class DB():
def __init__(self, username, passwd, EZconnstr):
print("create pool")
self.pool = cx_Oracle.SessionPool(user=username,
password=passwd,
dsn=EZconnstr,
min=4,
max=10,
increment=1,
threaded=True,
getmode=cx_Oracle.SPOOL_ATTRVAL_WAIT)
def query(self, query, binds=None, commit=False):
print("query")
# Acquire a connection from the pool
connection = self.pool.acquire()
# Use the pooled connection
cursor = connection.cursor()
cursor.execute(query, binds)
result = cursor.fetchall()
if commit:
connection.commit()
cursor.close()
# Release the connection to the pool
self.pool.release(connection)
return result
我想从我的 routes.py 中的那个类访问查询方法,但我无法让它工作......
在我的 simple_app/ init .py 中:
from flask import Flask
from flask_cors import CORS
from .routes.route_1 import r1_bp
from .routes.route_2 import r2_bp
from .lib.oracle import DB
def create_app():
app = Flask(__name__)
CORS(app)
app.secret_key = 'dev'
app.config.from_object('configSIMPLE')
app.register_blueprint(r1_bp)
app.register_blueprint(r2_bp)
db=DB(app.config['USER'], app.config['PASSWD'], app.config['DSN'])
return app
这是我的 simple_app/routes/route_2.py :
from flask import Blueprint, jsonify, current_app
#from .. import db
r2_bp = Blueprint('route_2', __name__)
@r2_bp.route('/route_2', methods=['GET'])
def route_2_get():
res = db.query('select * from dual', binds=None, commit=False)
print(res)
return jsonify(message = f'route_2 was run!'), 200
当我使用“flask run”命令启动我的应用程序工厂时
$ flask run
* Serving Flask app "simple_app" (lazy loading)
* Environment: development
* Debug mode: on
create pool
* Running on https://<host>:<port>/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 102-657-370
create pool
池已初始化,看起来还可以,但是当我使用 curl 访问 route_2 时:
$ curl -s -X GET "https://<host>:<port>/route_2"
我收到以下错误:
res = db.query('select * from dual', binds=None, commit=False)
NameError: name 'db' is not defined
这是正常的,因为我的 route_2.py 中没有相关的导入
但是当我取消注释#from .. import db in route2.py 我得到循环导入错误
from .. import db
ImportError: cannot import name 'db' from partially initialized module 'simple_app' (most likely due to a circular import) (in simple_app/__init__.py)
我应该怎么做才能让它发挥作用?
解决方案
推荐阅读
- html - 卡片布局中的内容在移动视图中不会缩小
- java - 多个 Spring @RequestMapping 标头
- javascript - 在 Eclipse Photon 中使用 async/await 验证错误
- c# - 三层c#asp.net数据库应用中OOP的正确使用
- spring-boot - Spring Boot 嵌入式 TomCat 添加执行器后无法启动
- amazon-cloudformation - 在 Cloudformation 中创建 Application Load Balancer 时出错... XXXXX 必须是 ARN 格式
- javascript - 数组内容未在 ReactJS 中呈现
- c# - 在c#中打破骆驼案例功能
- swift - 如果通过 selectedIndex 属性进行更改,则更改视图时的动画将无法正常工作
- python - Python:用项目列表替换列表中的项目