Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

debugging a uwsgi python application using pycharm

Is it possible to debug a uwsgi application using an ide like PyCharm? I can debug flask based apps fine by running them directly from pycharm but cannot even run a uwsgi app from within pycharm.

Do I have to use remote debugging? Is it possible to start a uwsgi app from within pycharm using run?

like image 334
remudada Avatar asked Jan 21 '14 12:01

remudada


People also ask

How do I enable debugging actions in PyCharm?

Just right-click any line in the editor and select the Debug <filename> command from the context menu. After the program has been suspended, use the debugger to get the information about the state of the program and how it changes during running.

Can you step through code in PyCharm?

Step through the program Stepping is the process of controlling step-by-step execution of the program. PyCharm provides a set of stepping actions, which are used depending on your strategy (for example, whether you need to go directly to the next line or enter the methods invoked on your way there).


2 Answers

You can still run your WSGI app outside of uWSGI for development and debugging purposes.

However sometimes this is not possible, for example if your app relies on uWSGI API features.

As far as I know you can't use "Attach to Process" from PyCharm because your WSGI app is running embedded into uWSGI, and there are no visible Python processes. Remote debugging however works like a charm.

  1. Locate pycharm-debug*.egg files in your PyCharm distribution. For example, on OSX both can be found in /Applications/PyCharm.app/Contents

  2. Copy pycharm-debug-py3k.egg next to your Flask app, or copy pycharm-debug.egg instead if you are using Python 2.7

  3. In PyCharm, create a "Python Remote Debug" configuration from "Run/Debug Configurations" dialog. In this example I use localhost and port 4444. This dialog will show you the corresponding pydevd.settrace(...) line.

  4. Add the following code to your app :

    import sys sys.path.append('pycharm-debug-py3k.egg')  # replace by pycharm-debug.egg for Python 2.7 import pydevd # the following line can be copied from "Run/Debug Configurations" dialog pydevd.settrace('localhost', port=4444, stdoutToServer=True, stderrToServer=True) 
  5. In PyCharm, start the remote debugging session. PyCharm's console should display the following line :

    Waiting for process connection... 
  6. Run your app from uWSGI as usual. It should attach to the debugger, and PyCharm's console should display :

    Connected to pydev debugger (build 139.711) 
  7. Your app should break on the pydevd.settrace(...) line. You can then continue and use PyCharm debugger as usual (breakpoints and so on)

like image 82
Julien Lirochon Avatar answered Sep 21 '22 17:09

Julien Lirochon


Not sure how to interpret your question, as you are mixing apples and oranges. Flask is a framework, uWSGI is an application server. I'll try to answer, though.

As far as I know, uWSGI is not pure python, so debugging it in PyCharm will not be trivial, if even it is possible.

However, since you are using uWSGI to run your application, I'm assuming it complies with the WSGI protocol. In that case, for debugging purposes, you can alternatively run it from a simple pure-python application engine like wsgiref.simple_server.WSGIServer.

like image 33
Pierre-Antoine Avatar answered Sep 18 '22 17:09

Pierre-Antoine