使用PyCharm调试器运行Flask CLI命令 [英] Run Flask CLI command with PyCharm debugger

查看:373
本文介绍了使用PyCharm调试器运行Flask CLI命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Flask中创建了自定义CLI命令,我可以通过终端中的flask my_command运行.我想使用PyCharm的调试器运行此命令.

I have created a custom CLI command in Flask, that I am able to run via flask my_command in the terminal. I want to run this command using PyCharm's debugger.

我创建了一个"Flask服务器"配置,并使用PyCharm调试器运行它,并在我在视图函数内部设置的断点处停止运行.但是,如果我尝试从PyCharm的终端运行CLI命令,它不会在命令的断点处停止.

I created a "Flask server" configuration, and running it with the PyCharm debugger stops at breakpoints I set inside view functions. But if I try to run my CLI command from PyCharm's terminal, it doesn't stop at breakpoints in the command.

我需要自定义配置来调试自定义CLI命令吗?我找到了关于Django命令的问题,但是PyCharm的"Flask服务器"配置没有相同的选项.如何配置PyCharm来调试Flask CLI命令?

Do I need a custom configuration to debug custom CLI commands? I found a question about Django commands, but PyCharm's "Flask server" configuration doesn't have the same options. How can I configure PyCharm to debug a Flask CLI command?

推荐答案

PyCharm的"Flask服务器"配置仅调用flask run命令,而没有提供调用其他命令的方法.为此,请创建一个常规的"Python"配置,该配置使用所需的参数运行flask命令.

PyCharm's "Flask server" configuration only calls the flask run command, it doesn't provide a way to call other commands. To do that, create a regular "Python" configuration that runs the flask command with the arguments you want.

  • 创建一个"Python"配置并为其命名.
  • 选择模块名称"而不是脚本路径",然后键入flask.
  • 填写"Parameters"以及要传递的参数.例如my_command --option A.
  • 编辑环境变量以包含FLASK_APP=my_appFLASK_ENV=development,与在终端上使用它们的方式相同.
    • 如果命令取决于从何处运行,则可能还需要配置工作目录"以指向项目目录.
    • Create a "Python" configuration and give it a name.
    • Select "Module name" instead of "Script path" and type flask.
    • Fill out "Parameters" with the arguments to pass. For example my_command --option A.
    • Edit the environment variables to include FLASK_APP=my_app and FLASK_ENV=development, the same way you'd use them from the terminal.
      • You may also need to configure "Working directory" to point at your project directory, if your command depends on where it's being run from.

      使用调试器运行此配置将在CLI命令中的断点处停止,而不是运行服务器.

      Running this configuration with the debugger will stop at breakpoints in your CLI command instead of running the server.

      这篇关于使用PyCharm调试器运行Flask CLI命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆