Debug flask in PyCharm

I am trying to figure out how to easily switch between debugging my Flask application with the Flask debugger (Werkzeug) and using the PyCharm debugger. I got down to two PyCharm launch configurations.

  • the one I call with the Run checkbox and --debug

    that comes in the application script; and
  • which I call with "Debug" and a flag --pydebug

    supplied to the application script

with flags supported in my app script using

if __name__ == "__main__":

    import argparse
    parser = argparse.ArgumentParser(description='Runs Web application with options for debugging')
    parser.add_argument('-d', '--debug', action='store_true', 
                        dest='debug_mode', default=False,
                        help='run in Werkzeug debug mode')
    parser.add_argument('-p', '--pydebug', action='store_true') 
                        dest='debug_pycharm', default=False,
                        help='for use with PyCharm debugger')

    cmd_args = parser.parse_args()
    app_options = dict()

    app_options["debug"] = cmd_args.debug_mode or cmd_args.debug_pycharm
    if cmd_args.debug_pycharm:
        app_options["use_debugger"] = False
        app_options["use_reloader"] = False

    application.run(**app_options)

      

This works and meets the requirement that I don't need to edit any code to deploy to the server; but I'm not sure if this is the best approach. On the one hand, I need to remember that the second configuration should be started with "Debug" (not "Run"), while the first one is meant to be started with "Run" (not "Debug") in PyCharm.

Is there a better approach to support these two approaches to debugging in PyCharm using some PyCharm function (for example, this allows me to detect when "Debug" is called and not "Run") or is it smarter to use Flask functions.

+3


source to share


1 answer


A very simple way to check if your application is running under the PyCharm debugger:



'pydevd' in sys.modules

      

+3


source







All Articles