Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug flask.app with pycharm 2.x that's running on gunicorn

I'm developing a flask.app that uses web socket functionality and installed flask-socket to provide that. So the flask-socket developer recommends gunicorn as web server. My question is now how to connect the remove debugger of pycharm with gunicorn to intercept the execution with breakpoints.

like image 249
arbyter Avatar asked Dec 22 '13 18:12

arbyter


People also ask

How do I debug a Python Flask in PyCharm?

Run/Debug Configuration: Flask Server Professional feature: download PyCharm Professional to try. Use this dialog to create run/debug configuration for Flask server and customize the way PyCharm executes your Flask application. See Creating web applications with Flask for more details on using Flask in PyCharm.

How do you debug a gunicorn?

The best you can do with gunicorn is to run it with gunicorn --debug testserver:app . That gives you the trace in addition to the Internal Server Error message. However, this is just the same text trace that you see in the terminal and not the Flask debugger.

How do you debug Flask in PyCharm Community Edition?

Click the list of the available configurations and select Edit Configurations. In the Flask Run/Debug configuration dialog, select the FLASK_DEBUG checkbox. With this mode enabled, PyCharm will restart the running application each time you save the code changes Ctrl+S .

How do I run gunicorn in PyCharm?

Go to run/debug configurations creating-and-editing-run-debug-configurations. Choose new "Python" config. Script path: your_path_to_/venv/bin/gunicorn. Parameters(for my case): -b :5001 --access-logfile - --error-logfile - "run:create_application()"


2 Answers

Settings > Project Settings > Python Debugger

There's an option in there to enable "gevent compatible debugging".

Then, go into your debugger settings (shortcut is through the toolbar, click the dropdown near the play/debug icons and select "Edit Configurations"

Set the "Script" to your virtualenv's isntallation of gunicorn, something like:

/Users/iandouglas/.virtualenvs/defaultenv/bin/gunicorn

Set the "Script Parameters" to something like -b 192.168.1.1:9000 app:yourappname (assuming your primary starting script is called app.py and you're refering to it as 'yourappname'

the "Working directory" will be automatically set otherwise set it to wherever your code lives: /Users/iandouglas/PycharmProjects/MyExampleApp

I have a separate config file for my gunicorn settings, which specifies a host/port but I still had to specify the -b 0.0.0.0:5001 parameter to force gunicorn to bind to all IPs on my machine on port 5001.

p.s.

One important step is to add this envvar as pointed out here

PYDEVD_USE_CYTHON=NO
like image 172
iandouglas Avatar answered Oct 19 '22 21:10

iandouglas


My case for PyCharm 2018.1.3 Professional:

  1. Go to run/debug configurations creating-and-editing-run-debug-configurations

  2. Choose new "Python" config

  3. Script path: your_path_to_/venv/bin/gunicorn
  4. Parameters(for my case): -b :5001 --access-logfile - --error-logfile - "run:create_application()"
  5. Python interpreter: your venv python version for project
  6. Working directory: path to your project
  7. Save and press DEBUG (Shift+F9)
  8. be happy!
like image 12
MrNinjamannn Avatar answered Oct 19 '22 21:10

MrNinjamannn