【发布时间】:2020-06-25 08:32:02
【问题描述】:
我有一个 Flask 项目,其中入口点是 application.py,然后我有几个其他模块,例如, variant.py等
项目结构为:
>my_app_dir/
application.py
views/
__init__.py
users.py
variant.py
...
对于variant.py,它是一个类似的函数:
import ...
from views import *
def variant(variant_id, subset='all', language='en'):
...
if subset == 'all':
return json.dumps(x)
return json.dumps([{subset: y[subset]} for y in x])
关键是我想像 API 一样使用variant.py,所以我正在通过iPython 进行测试,但它返回错误:
from views import variant as v
aa = v.variant('22-38212762-A-G')
...
RuntimeError: Working outside of request context.
This typically means that you attempted to use functionality that needed
an active HTTP request. Consult the documentation on testing for
information about how to avoid this problem.
我试过谷歌搜索,但找不到任何类似的案例,但我尝试了几件事无济于事。
【问题讨论】:
-
您想像 API 一样使用
variant.py吗?我期待您向端点发出http请求。还不是很清楚。这个线程可能有用stackoverflow.com/questions/9931738/… -
该代码适用于 Web 应用程序。由于
variant.py正在访问我们的 postgres 数据库,我可能希望在本地测试或快速调试以及在 iPython 中重用这个函数,我觉得很舒服。