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

查看:44
本文介绍了使用 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.
  • 用要传递的参数填写参数".例如 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天全站免登陆