Debug Pylons application through Eclipse - python

I have Eclipse setup with PyDev and love being able to debug my scripts/apps. I've just started playing around with Pylons and was wondering if there is a way to start up the paster server through Eclipse so I can debug my webapp?

Create a new launch configuration (Python Run)
Main tab
Use paster-script.py as main module (you can find it in the Scripts sub-directory in your python installation directory)
Don't forget to add the root folder of your application in the PYTHONPATH zone
Arguments
Set the base directory to the root folder also.
As Program Arguments use "serve development.ini" (or whatever you use to debug your app")
Common Tab
Check allocate console and launch in background

If you'd rather not include your Python installation in your project's workspace to get paster, you can create a pure-Python driver like:
#!/usr/bin/env python
from paste.script.serve import ServeCommand
ServeCommand("serve").run(["development.ini"])
...and run/debug that in Eclipse.
Note: this is running without the --reload option, so you don't get hot deploys (i.e., you'll need to reload server to see changes). Alternatively, you can add the --reload option to get hot deploys, but then Pydev won't stop at your breakpoints. Can't have your cake and eat it too...
ServeCommand("serve").run(["--reload", "development.ini"])

yanjost has it right, just wanted to add that you need to make sure you do not use the --reload option, this will prevent the debugger from properly attaching itself and cause your breakpoints not to work. Just a little thing I ran in to.

I was able to get --reload working by changing the 'Working directory' in the arguments tab to not use default (i.e. select 'Other'->File System->'Root of your Pylons' app where development.ini is stored.

On linux that will probably be /usr/bin/paster or /usr/local/bin/paster for paste script, and for arguments i have: serve ${workspace_loc}${project_path}/development.ini

I also got this working (finally). I used buildout instead of virtualenv to install pylons (instructions at: http://wiki.pylonshq.com/display/pylonscommunity/Howto+install+Pylons+with+buildout), so the instructions above needed to be changed a little as far as the paths go.
-for "Main Module", I use:
${workspace_loc:myeclipseprojectname/bin/paster}
(adding --reload made breakpoints not work for me, and I tested this a couple times)
-for "Program Arguments", I use:
serve ${workspace_loc:myeclipseprojectname/mypylonsprojectname/development.ini}
-for "Working Directory, Other:", I use:
${workspace_loc:myeclipseprojectname/mypylonsprojectname}
-as mentioned above, in "Common Tab", "Check allocate console and launch in background"
-and remember to set a breakpoint before trying.

This doesn't really answer question about how to do it in eclipse. But I've been debugging paster server with winpdb, which is quite nice graphical python debugger (you can install it with easy_install winpdb).
Just start your server e.g.:
winpdb /usr/local/bin/paster serve development.ini
And click run button.
As wayne said, it's necessary to not use --reload option. At least I wasn't able to find how to attach to actual webapp even, when selecting to which forked process debugger should enter (entering different processes can be controlled with "fork parent" and "fork child" debugger commands).

Related

How to debug a python script launched by a third party app

I'm using Linux Eclipse (pydev) as IDE to develop python scripts that are launched by an application written in C++. I can debug the python script without problems in the IDE, but the environment is not real (the C++ program sends and receives messages through the stdin/stdout and it's a complex communication channel that I can't fully reproduce writing the messages by hand).
Until now I was using log messages to debug (poor man's debug) but it's getting too complex. When I do something similar in PHP I can just leave xdebug listening and add breakpoints in Netbeans. Very neat and easy. Is it possible to do something like that in Python 3.X (with Eclipse or other IDE)?
NOTE: I know there is a Pydev / Attach to Process functionality, but it doesn't work. Always fails to attach.
NOTE2: There is also a built-in "breakpoint()" in Python 3.7 but it links to a debugger and if also fails, the IDE never gets the control.
After some research, this is the best option I have found. Without any other solution provided, I post it just in case anyone has the same problem.
Python has an integrated debugger: pdb. It works as a module and it doesn't allow to use it if you don't have the window control (i.e. you launch the script).
To solve this there are some coders that have created modules that add a layer on pdb. I have tried some and the most easy and still visual interesting is rpudb (but have a look also to this).
To install it:
pip3 install https://github.com/msbrogli/rpudb/archive/master.zip
(if you install it using the pip3 install rpudb command it will install an old version only valid for python 2)
Then, you use it just adding an import and a function call:
import rpudb
.....
rpudb.set_trace('127.0.0.1', 4444)
.....
Launch the program and it will stop in the set_trace call. To debug it (and continue) open a terminal and launch a telnet like this:
telnet 127.0.0.1 4444
You will have a visual debugger in front of you with the advantage that you can not only debug local programs, but also remote (just change the IP).
I was able to attach PyCharm to a running python process and use break points using PyCharm attach to process
I created a bash script which exec a python script, should work the same with C++

PyCharm integrated debugger slows down application

I am using PyCharm to debug a moderately complex Pyramid web application with a lot of dependencies. When I run the application inside PyCharm using PyCharm's Debug run, the application start up slows down significantly. This kills the normal web application workflow of edit, save, refresh. The slowdown is significant, making the application restart to take tens of seconds instead of fractions of seconds.
Is there a way to speed up PyCharm debug runs any way? The similar slowdown would not occur if one is using hardcoded import pdb ; pdb.set_trace() style breakpoints and normal Run mode.
The way to get fast debugging sessions in PyCharm (Professional edition) is to use remote debugging, similar to pdb.set_trace().
In the Run/Debug Configurations dialogue, create a Remote Debug configuration. The dialogue contains the instructions, which I will repeat here completeness sake:
Add pycharm-debug.egg from the PyCharm installation to the Python path.
Add the following import statement:
import pydev
Add the following command to connect to the debug server:
pydevd.settrace('localhost', port=$SERVER_PORT, stdoutToServer=True, stderrToServer=True)
These strings can be copied from the dialogue and pasted into the source. When you choose the host and server port in the dialogue, the pasteable strings will update themselves. Of course, they can also be concatenated to a oneliner using ;.
After the settrace() method has been run, the breakpoints you have set in PyCharm will become active.
So, where's the file pycharm-debug.egg? Somewhere in the near vicinity of the PyCharm binary. In OS X, you will find the file within the Contents/debug-eggs directory within PyCharm.app. I assume other PyCharm distributions have a similar directory.
If you're running the application using a virtualenv, install the egg using easy_install.
If you prefer to run your application within PyCharm (stdout in the PyCharm console is useful), then add the path to the egg file to the Project Interpreter's file paths.

debug Blender Python in Eclipse and PyDev?

How do you go about debug Blender Python in Eclipse and PyDev?
What I have tried is:
http://www.luxrender.net/wiki/LuxBlend25_Debugging_with_eclipse
http://www.blender.org/forum/viewtopic.php?t=3914&sid=717a127d12596f89e4aea0c54938ef80
But non of then seams to work?
Regards
There is very good e-book written by Witold Jaworski about Blender add-on programming. It includes chapters with step by step instructions how to setup Eclipce with PyDev to debug Blender add-ons.
Programming Add-ons for Blender 2.5
Here is how I setup debug, which is slight different but based on the lux-render tutorial.
First, create the a .py file, lets call it debug.py, which will contain a function which we will call later to setup debugging. Put this file in the same folder as the main __init__.py of your module. As per the lux-renderer tutorial, add the following code, updating PYDEV_SOURCE_DIR.
import sys
def startdebug():
try:
# set the PYDEV_SOURCE_DIR correctly before using the debugger
PYDEV_SOURCE_DIR = 'C:\Program Files\eclipse\plugins\org.python.pydev.debug_2.5.0.2012040618\pysrc'
# test if PYDEV_SOURCE_DIR already in sys.path, otherwise append it
if sys.path.count(PYDEV_SOURCE_DIR) < 1:
sys.path.append(PYDEV_SOURCE_DIR)
# import pydevd module
import pydevd
# set debugging enabled
pydevd.settrace(None, True, True, 5678, False, False)
except:
pass
When setting the PYDEV_SOURCE_DIR ensure you point it to the org.python.pydev.debug_xxxxx. There is another folder similiar to this. To ensure you have the correct folder it will contain a /pysrc folder.
Now in your main __init__.py, this must come before any other import statements to work correctly. Add the following directly under the bl_info section, as strangely blender parses this itself.
DEBUGGING = True
if(DEBUGGING):
import debug
debug.startdebug()
Having it here will avoids adding per file traces like the lux-render tutorial.
Add some breakpoint to the version in the add-ons folder,
Switch to the debug perspective,
Start Eclipses debug server,
Start blender
Run the script and it will hit the breakpoint.
The common problems I find people encounter:
pointing the path to the wrong pydev debug folder, ensure that there is a /pysrc folder
When Pydev updates, update the PYDEV_SOURCE_DIR as the debug_xxxxx will have change
not having eclipse server running,
setting breakpoints on a local copy of the files instead of the version in the blender add-on directory
saving the script does not mean that blender will reload it, use imp, disable/renable the add-on or restart Blender.
There are quite comprehensive instructions for setting up blender and eclipse for debugging.
http://wiki.blender.org/index.php/User:Z0r/PyDevAndProfiling
While this is for blenders game engine, much of it applies to regular blender.

Python IDE on Linux Console

This may sound strange, but I need a better way to build python scripts than opening a file with nano/vi, change something, quit the editor, and type in python script.py, over and over again.
I need to build the script on a webserver without any gui. Any ideas how can I improve my workflow?
put this line in your .vimrc file:
:map <F2> :w\|!python %<CR>
now hitting <F2> will save and run your python script
You should give the screen utility a look. While it's not an IDE it is some kind of window manager on the terminal -- i.e. you can have multiple windows and switch between them, which makes especially tasks like this much easier.
You can execute shell commands from within vim.
Using emacs with python-mode you can execute the script with C-c C-c
you can try ipython. using its edit command, it will bring up your editor (nano/vim/etc), you write your script, and then on exiting you're returned to the ipython prompt and the script is automatically executed.
When working with Vim on the console, I have found that using "tabs" in Vim, instead of having multiple Vim instances suspended in the background, makes handling multiple files in Vim more efficient. It takes a bit of getting used to, but it works really well.
You could run XVNC over ssh, which is actually passably responsive for doing this sort of thing and gets you a windowing GUI. I've done this quite effectively over really asthmatic Jetstart DSL services in New Zealand (128K up/ 128K down =8^P) and it's certainly responsive enough for gvim and xterm windows. Another option would be screen, which lets you have multiple textual sessions open and switch between them.
There are actually 2 questions. First is polling for a console IDE for python and the second is a better dev/test/deploy workflow.
For while there are many ways you can write python code in the console, I find a combination of screen, vim and python/ipython is the best as they are usually available on most servers. If you are doing long sessions, I find emacs + python-mode typically involves less typing.
For a better workflow, I would suggest setting up a development environment. You can easily setup a Linux VM on your desktop/laptop easily these days - there isn't a excuse not to even if it's for hobby projects. That opens up a much larger selection of IDEs available to you, such as:
GUI versions of VI and friends
Remote file editing with tramp and testing locally with python-mode inside Emacs
http://www.netbeans.org
and of course http://eclipse.org with the PyDev plugin
I would also setup a SCM to keep track of changes so that you do
better QA and use it to deploy tested changes onto the server.
For example I use Mercurial for my pet projects and I simply tag my repo when it's ready and update the production server to the tag when I deploy. On the devbox, I do:
(hack hack hack, test test test)
hg ci -m 'comment'
hg tag
hg push
Then I jump onto the server and do the following when I deploy:
hg update
restart service/webserver as needed
Well, apart from using one of the more capable console editors (Emacs or vi would come to mind), why do you have to edit it on the web server itself? Just edit it remotely if constant FTP/WebDAV transfer would seem to cumbersome.
Emacs has Tramp Mode, gedit on Linux and bbedit on the Mac support remote editing, too. Probably quite a large number of other editors. In that case you would just edit in on a more capable desktop and restart the script from a shell window.
For what it's worth, VIM alone can do the same tasks as previously posted. I have had the same problem with testing Python from the command line.
My solution was to use the screen command. I split screens vertically, I run Python in one instance of a shell, and on the second screen, I usually edit Python code with VIM.
Command to install screen:
sudo apt-get install screen
The screen package has a bit of a learning curve but there isn't any mystery if you can remember the "Ctrl-Alt ?" command that contains all knowledge.
No GUI is required!

How to debug Web2py applications?

Is it possible?
By debug I mean setting breakpoints, inspect values and advance step by step.
You can do remote debugging of python web apps over TCP/IP with winpdb.
(Link appears down as of June 2019. Try PyPI winpdb)
I haven't used web2py, but if it runs in a terminal window, you can use standard pdb stuff. Add this line somewhere in your code:
import pdb; pdb.set_trace()
This will invoke the debugger and break. Then you can use PDB commands: n to step to the next line, l to list code, s to step into a function, p to print values, etc.
One can debug applications built on Web2py using the following set-up:
Eclipse IDE
Install Pydev into Eclipse
Set Breakpoints on your code as needed
Within Eclipse right-click the file web2py.py and select Debug As -> Python Run
When a breakpoint is hit Eclipse will jump to the breakpoint where you can inspect variables and step thru the code
You can also use Visual Studio 2010. Here's how:
Download and install Python Tools for Visual Studio.
Create a new project from existing code (File > New > Project From Existing Code...)
Specify your web2py folder and use the defaults.
Right-click on web2py.py and choose Set as Startup File.
Set breakpoints and hit F5 (run) or right-click on web2py.py and choose Start with Debugging.
This is a nice setup if you already use visual studio.
Yes, it is possible, Due to the "span prevention" I am still not allowed to post screenshots, but here is a full screenshot hosted at my website:
http://static.techfuel.net/debug_web2py.png
I'm debugging web2py applications with Eclipse and PyDev. Here is an article:
http://www.web2pyslices.com/main/slices/take_slice/2
Here is an article on debugging python with pdb, which will work with web2py. http://sontek.net/debugging-python-with-pdb
As Carl stated, it is as easy as:
Installing PyDev in Eclipse
Right Click on your Web2Py project, selecting Debug As > Python Run
Selecting web2py.py as the file to run
No other plugins or downloads are needed.
#Ned Batchelder is almost right, but the standard way of doing it in web2py is slightly different.
Instead of `import pdb; pdb.set_trace(), you use the code:
from gluon.debug import dbg
dbg.set_trace()
When executing the web application, the application will freeze when it reaches this section of code. You then go to http://127.0.0.1:8000/admin/debug/interact (using the root URL for your application) and it will show a fully interactive, web based debugger:
See documentation.

Categories

Resources