Joel
Joel

Reputation: 111

How to run Flask CLI from within PyCharm under Windows

I've been trying to get the Flask CLI to debug from within PyCharm with no success. I've tried the recommended procedures listed here. However, this doesn't work under Windows since flask.exe is an executable and not a script. Renaming flask.exe to flask does not work either. This causes the following exception:

enter image description here

Jetbrains has an active incident report in YouTrack for this, but there's been no activity done on it yet, with the Kanban State set to "Not On Board", so it looks like it's going to be sometime before the issue is addressed.

Any help would be greatly appreciated.

EDIT: Using Pycharm run works properly. Trying to run using the Pycharm debugger causes the exception

EDIT 2: Results after creating flask_debug.py file as recommended: enter image description here

Upvotes: 4

Views: 2383

Answers (3)

svvitale
svvitale

Reputation: 111

In the latest version of PyCharm, there's an option to run by Module name instead of by file. Using "flask" as the module name works as well and doesn't require you to create a flask_debug.py file.

enter image description here

Upvotes: 3

Joel
Joel

Reputation: 111

I'm attaching the screenshot of the working fileset and the Run/Debug Configuration for reference. This answer is thanks to the support provided by pjcunningham.

enter image description here

Upvotes: 0

pjcunningham
pjcunningham

Reputation: 8046

Firstly, rename flask.py back to flask.exe.

In PyCharm's Run Configuration dialog manually enter the full path to the Flask executable in the Script: text box. Don't use the browse button as it filters on Python scripts (.py files).

See screenshot. In this instance there is a virtual environment called "href" and the flask executable is in the Scripts sub-directory.

enter image description here

To use PyCharms's debugger create a file in the root say flask_debug.py:

from flask.cli import main

if __name__ == '__main__':
    main(as_module=False)

Then setup PyCharm to run this script passing any Flask CLI parameters as required. See screenshot below showing Run/Debug configuration and the debugger stopped at a breakpoint.

enter image description here

Below shows Flask 0.12.2 quickstart app running under the PyCharm debugger and showing the defined environmental variable FLASK_APP.

enter image description here

Upvotes: 4

Related Questions