Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
466 views
in Technique[技术] by (71.8m points)

python - Run Flask CLI command with PyCharm debugger

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.

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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

  • 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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...