Running Python in debug mode under Eclipse
Download and install Eclipse
Even though you will be developing Python you will still need Eclipse with the Java SDK. I think this is for 2 reasons. Firstly Python support for Eclipse is in the form of an Eclipse plug-in as opposed to a fully integrated Eclipse application and there aren't any Eclipse distributions with no (computer) language support. The other reason (I think) is that many of the development tools for Java are used for Python.
- Go to the Eclipse download page: http://download.eclipse.org/eclipse/downloads/
- From the Latest Release section download the latest version 3.6.1 (eclipse-SDK-3.6-win32.zip)
There is no installer so we will have to do it ourselves, but don't worry it's dead easy.
- Create a folder for your python development installation. You will save yourself a lot of trouble if you install the application under a directory without a space in the directory path name:
mkdir c:\python
- Unzip the downloaded zip in this folder, so you have a folder called
c:\python\eclipse
Download and install Python
There are 2 versions of Python: 2.7 and 3.1, both are considered stable but there are more compatible 3rd party libraries for 2.7 than there are for 3.1 so download that instead.
- Go to the Python download site: http://www.python.org/download/releases/2.7/
- Download the Windows MSI installer
- Install it in the default location:
c:\python27
.
Download and install PyDev
Still with me? Now we're going to download the PyDev (Python plug-in for Eclipse).
- Launch Eclipse by double clicking on the eclipse executable located at:
c:\python\eclipse\eclipse.exe
- When it launches for the first time, it will ask you to provide the location of the workspace, so pick a location without spaces in the path and not under the eclipse root (
c:\python\eclipse
). I'm going to keep everything together and set my workspace toc:\python\workspace
.
Next we are going to install the PyDev plug-in.
- From the Eclipse menu bar Help -> Install New Software...
- Click the Add button and enter the details of the PyDev repository.
- Set Name to Pydev and Pydev Extensions
- Set Location to http://pydev.org/updates
- Then click OK
- After a few seconds PyDev and PyDev Mylyn Integration (optional) will appear. Click the Select All button and then click Next.
- Eclipse will work out the dependencies (which in this case is none) and allow you to click Next again.
- Accept the licence agreements, click Finish and go and get yourself a coffee.
- There were a couple of warnings about unsigned content, so just click through these.
Configuring Eclipse and Python
Now that everything is installed we must tell Eclipse about Python.
- Go to: Window -> Preferences -> Pydev -> Interpreter - Python
- Click New...
- Enter Python 2.7 in the Interpreter Name and use the browse button and navigate to the
python.exe
executable to fill in the Interpreter Executable field. - When you're done click Ok.
- Clicking Ok will make the Pydev perform a search for python bits.
Test Eclipse / Python installation
Now to test our installation we are going to write a little program.
- From the Eclipse menubar File -> New -> Project....
- Select PyDev -> Pydev Project, and click Next.
- Enter a Project name of HelloWorld.
- Then click Finish.
- You'll be asked about switching to the Pydev perspective, so click Yes because this will make it easier to develop under.
Before we can right real code we need a Python module to put it in.
- Highlight the HelloWorld project and from the Eclipse menu bar select File -> New -> PyDev Package.
- Set the Source Folder to /HelloWorld/src.
- Set the Name to uk.co.bigsoft.python.
- The click Finish
I have just come from developing under Java which encourages back-domain-name packages, so I have done the same here. Most Python projects are only one or two packages deep.
Next up we are going to create a module in our package.
- Highlight the
Python
package and select File -> New -> Pydev module. - For the Name field enter helloworld.
- For the Template highlight Module: Main.
- Then click Finish
Next we are going to write a simple python program:
'''
Created on 6 Sep 2010
@author: MrN
'''
if __name__ == '__main__':
c = 0
a = 5
b = 2
c = a + b
print c
Now that we have a program we are going to set up the debugger.
- Hover in the margin next to line
b = 2
, right click and select Add Breakpoint. This will put a green spot in the margin. - From the menu bar select Run -> Debug.
- If the Debug As window pops up asking you to Select a way to debug 'helloworld.py', then select Python Run, and then click Ok.
- This will ask us if we want to open a Debug perspective, so click Yes and Remember my decision otherwise it will keep pestering you.
The program will launch in the debug perspective and stop at the breakpointed line (indicated with the green spot). On the right hand side make sure the Variables tab is showing and it will have our 2 variables defined: a and c valued at 5 and 0 respectively. Repeating Run -> Step over will progress the run line by one line of code.
Well done.
No feedback yet
Form is loading...