我在PyCharm中创建了新的flask项目,但看不到如何在集成的PyCharm python控制台窗口中运行flask shell。
我仍然可以在集成的PyCharm终端中运行命令“ flask shell”,但是它将在没有代码完成,提示,语法检查等的情况下启动。
Flask应用程序在终端中定义:
答案 0 :(得分:1)
您可以结合使用creating a request context for the Shell(flask shell
和custom starting script for the PyCharm console)。
# Step 1: acquire a reference to your Flask App instance
import app
app_instance = app.create_app()
# I return app instance from a factory method here, but might be be `app.app` for basic Flask projects
# Step 2: push test request context so that you can do stuff that needs App, like SQLAlchemy
ctx = app_instance.test_request_context()
ctx.push()
现在,您应该拥有flask shell
的好处,以及代码完成和IPython Shell提供的其他好处,所有这些都来自默认的PyCharm Python Shell。
答案 1 :(得分:0)
您可以在PyCharm here中阅读有关 Flask 配置的更多信息。
简而言之,您需要使用工作目录和环境变量来创建新配置,例如flask run
。您可以复制创建的配置,只需将脚本参数编辑为flask shell
。
答案 2 :(得分:0)
要运行交互式Python Shell,可以使用shell命令:
flask shell
这将启动一个交互式Python shell,设置正确的 应用程序上下文并在外壳中设置局部变量。这个 通过调用以下代码的Flask.make_shell_context()方法来完成 应用。默认情况下,您可以访问您的应用和g。
这只是一个普通的交互式Python shell,已经为您设置了一些局部变量,而不是具有代码完成,提示,语法检查等功能的IDE编辑器。
答案 3 :(得分:0)
就我而言(在我的server.py
中有app=Flask(__name__)
)我使用
from server import app
ctx = app.app_context()
ctx.push()
然后将其添加到File | Settings | Build, Execution, Deployment | Console | Python Console
起始脚本中
答案 4 :(得分:0)