Python Development With PyDev and Eclipse
Python Development With PyDev and Eclipse
1. Overview
1.1. What is Python
Python is an interpreted programming language and claims to be a very effective programming
language. Python was develop by Guido van Rossum.
The name Python is based on the TV show called Monty Python's Flying Circus. During execution
the Python source code is translated into bytecode which is then interpreted by the Python
interpreter. Python source code can also run on the Java Virtual Machine, in this case you are using
Jython.
Key features of Python are:
2. Installation
2.1. Python
Download Python from http://www.python.org. Download the version 3.3.1 or higher of Python. If
you are using Windows you can use the native installer for Python.
2.2. Eclipse Python plugin
The following assume that you have already Eclipse installed. For an installation description of
Eclipse please see Eclipse IDE for Java.
For Python development under Eclipse you can use the PyDev Plugin which is an open source
project. Install PyDev via the Eclipse update manager via the following update site:
http://pydev.org/updates
2.3. Configuration of Eclipse
You also have to maintain in Eclipse the location of your Python installation. Open in the Window
Preference Pydev Interpreter Python menu.
Press the New button and enter the path to python.exe in your Python installation directory. For
Linux and Mac OSX users this is normally /usr/bin/python.
Warning
Note that on MAC OSX the python libraries are not installed by default. Please use Google to find
out, how to install them on a Mac OSX system, the author of this text only uses Linux.
The result should look like the following.
Create a new project with the name "de.vogella.python.first". Select Python version 2.6 and your
interpreter.
Press finish.
Select Window->Open Perspective ->Other. Select the PyDev perspective.
Select the "src" folder of your project, right-click it and select New -> PyDev Modul. Create a
module "FirstModule".
Congratulations! You created your first (little) Python modul and ran it!
4. Debugging
Just right-click in the source code and add a breakpoint.
You can now inspect and modify the variables in the variables view.
Via the debug buttons (or shortcuts F5, F6, F7, F8) you can move in your program.
You can use F5 / F6, F7 and F8 to step through your coding.
Table 1. Debugging Key bindings
Command Description
F5
Goes to the next step in your program. If the next step is a method / function this
command will jump into the associated code.
F6
F6 will step over the call, e.g. it will call a method / function without entering the
associated code.
F7
F7 will go to the caller of the method/ function. So this will leave the current code and
go to the calling code.
F8
You can of course use the ui to debug. The following displays the key bindings for the debug
buttons.
5. Programming in Python
5.1. Comments
The following create a single line comment.
# This is a comment
5.2. Variables
Python provides dynamic typing of its variables, e.g. you do not have to define a type of the
variable Python will take care of this for you.
# This is a text
s= "Lars"
# This is an integer
x = 1
y=4
z=x+y
5.3. Assertions
Python provides assertions. These assertions are always called.
assert(1==2)
Description
len(s)
s[i]
s[-i]
Get the i-tes Sign of the string from behind the string, e.g. -1 returns the last element
in the string
Concatenates the int varibles a, b,c, e.g. if a=1, b=2, c=3 then the result is 123.
s.lower()
s.upper()
For example:
s = "abcdefg"
assert (s[0:4]=="abcd")
assert (s[4:]=="efg")
assert ("abcdefg"[4:0]=="")
assert ("abcdefg"[0:2]=="ab")
5.8. Lists
Python has good support for lists. See the following example how to create a list, how to access
individual elements or sublists and how to add elements to a list.
'''
Created on 14.09.2010
@author: Lars Vogel
'''
mylist = ["Linux", "Mac OS" , "Windows"]
# Print the first list element
print(mylist[0])
# Print the last element
# Negativ values starts the list from the end
print(mylist[-1])
# Sublist - first and second element
print(mylist[0:2])
# Add elements to the list
mylist.append("Android")
# Print the content of the list
for element in mylist:
print(element)
If you want to remove the duplicates from a list you can use:
mylist = ["Linux", "Linux" , "Windows"]
# remove duplicates from the list
mylist = list(set(mylist))
The following reads the same file but write the output to another file.
'''
@author: Lars Vogel
'''
f = open('c:\\temp\\wave1_new.csv', 'r')
output = open('c:\\temp\\sql_script.text', 'w')
for line in f:
output.write(line.rstrip() + '\n')
f.close()
__add__ + Operator
__mul__ * Operator