Python Development with Pydev and Eclipse - Tutorial

Python Development with Pydev and Eclipse - Tutorial

Python Development with PyDev and Eclipse - Tutorial http://www.vogella.com/articles/Python/article.html vogella.com Tutorials Training Services Publications Connect 130 Python Development with PyDev and Eclipse - Free tutorial, donate Tutorial to support Lars Vogel Version 1.8 Copyright © 2009, 2010, 2011, 2012 Lars Vogel 01.07.2012 Revision History Revision 0.1 12.01.2009 Lars Created by Lars Vogel Vogel Revision 0.2 - 1.8 31.01.2009 - 01.07.2012 Lars bug fixes and enhancements Vogel Python, Pydev and Eclipse This article describes how to write and debug Python programs with Eclipse This article was developed on Eclipse 3.6 (Helios), Python 2.6.2 and PyDev version 1.6.2. Table of Contents 1. Overview 1.1. What is Python 1.2. Block concept in Python- Indentation 1.3. About this tutorial 2. Installation 2.1. Python 2.2. Eclipse Python Plugin 2.3. Configuration of Eclipse 3. Your first Python program in Eclipse 4. Debugging 5. Programming in Python 5.1. Comments 5.2. Variables 5.3. Assertions 5.4. Methods / Functions in Python 5.5. Loops and if clauses 5.6. String manipulation 5.7. Concatenate strings and numbers 5.8. Lists 5.9. Processing files in Python 5.10. Splitting strings and comparing lists. 5.11. Writing Python Scripts in Unicode 5.12. Classes in Python 6. Google App Engine 7. Thank you 8. Questions and Discussion 9. Links and Literature 1. Overview BACK TO TOP 1.1. What is Python 1 of 11 4/4/2013 9:41 PM Python Development with PyDev and Eclipse - Tutorial http://www.vogella.com/articles/Python/article.html vogella.com Tutorials Training Services Publications Connect language. Python was develop from Guido van Rossum. The name Python is based on the TV show "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: high-level data types, as for example extensible lists statement grouping is done by indentation instead of brackets variable or argument declaration is not necessary supports for object-orientated, procedural and / or functional programming style 1.2. Block concept in Python- Indentation Python follows a different way then other programming languages to identify blocks on related code. A block is identified by indentation. If you have an if statement and the next line is indented then it means that this indented block belongs to the if. The Python interpreter supports either spaces or tabs, e.g. you can not mix both. The most "pythonic" way is to use 4 spaces per indentation level. 1.3. About this tutorial This tutorial will first explain how to install Python and the Python plugins for Eclipse. It will then create a small Python project to show the usage of the plugin. Afterwards the general constructs of Python are explained. 2. Installation 2.1. Python Download Python from http://www.python.org . Download version 2.6.x from 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 menu Window -> Preference and select Pydev-> Interpreter Python BACK TO TOP 2 of 11 4/4/2013 9:41 PM Python Development with PyDev and Eclipse - Tutorial http://www.vogella.com/articles/Python/article.html vogella.com Tutorials Training Services Publications Connect Press new and maintain the path to "python.exe" in your installation directory. The result should look like the following. BACK TO TOP 3 of 11 4/4/2013 9:41 PM Python Development with PyDev and Eclipse - Tutorial http://www.vogella.com/articles/Python/article.html vogella.com Tutorials Training Services Publications Connect 3. Your first Python program in Eclipse Select File -> New -> Project. Select Pydev -> Pydev Project. BACK TO TOP Create a new project with the name "de.vogella.python.first". Select Python version 2.6 and your interpreter. 4 of 11 4/4/2013 9:41 PM Python Development with PyDev and Eclipse - Tutorial http://www.vogella.com/articles/Python/article.html vogella.com Tutorials Training Services Publications Connect 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". BACK TO TOP 5 of 11 4/4/2013 9:41 PM Python Development with PyDev and Eclipse - Tutorial http://www.vogella.com/articles/Python/article.html vogella.com Tutorials Training Services Publications Connect Create the following source code. ''' Created on 18.06.2009 @author: Lars Vogel ''' def add(a,b): return a+b def addFixedValue(a): y = 5 return y +a print add(1,2) print addFixedValue(1) Right-click your model and select Run As -> Python run. Congratulations! You created your first (little) Python modul and ran it! 4. Debugging Just right-click in the source code and add a breakpoint. BACK TO TOP 6 of 11 4/4/2013 9:41 PM Python Development with PyDev and Eclipse - Tutorial http://www.vogella.com/articles/Python/article.html vogella.com Tutorials Training Services Publications Connect Then select Debug as -> Python Run 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 Use F8 to go to the next breakpoint. If no further breakpoint is encountered then the program will normally run. BACK TO TOP You can of course use the ui to debug. The following displays the keybindings for the debug buttons. 7 of 11 4/4/2013 9:41 PM Python Development with PyDev and Eclipse - Tutorial http://www.vogella.com/articles/Python/article.html vogella.com Tutorials Training Services Publications Connect 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) 5.4. Methods / Functions in Python Python allows to define methods via the keyword def. As the language is interpreted the methods need to be defined before using it. def add(a,b): return a+b print add(1,2) 5.5. Loops and if clauses The following demonstrates a loop the usage of an if-clause. i = 1 forforfor i in range(1, 10): ififif i <= 5 : print 'Smaller or equal then 5.\n' , elseelseelse : print 'Larger then 5.\n' , 5.6. String manipulation Python allows the following String operations. Table 2. Operations Description len(s) Returns the length of string s BACK TO TOP s[i] Gets the element on position i in String s, position start with zero 8 of 11 4/4/2013 9:41 PM Python Development with PyDev and Eclipse - Tutorial http://www.vogella.com/articles/Python/article.html vogella.com Tutorials Training Services Publications Connect s[-i] Get the i-tes Sign of the string from behind the string, e.g. -1 returns the last element in the string "abcdefg"[0:4] Gets the first 4 elements (abcd) "abcdefg"[4:] Gets the elements after the first 4 elements (abcd) `a`+`b` + `c` Concatenates the int varibles a, b,c, e.g. if a=1, b=2, c=3 then the result is 123. s.lower() Result will be s in lower cases s.upper() Result will be s in upper cases s.startswith(t) True, if s startsWith t s.rstrip() Removes the end of line sign from the string For example: s = "abcdefg" assert (s[0:4]== "abcd" ) assert (s[4:]== "efg" ) assert ( "abcdefg" [4:0]== "" ) assert ( "abcdefg" [0:2]== "ab" ) 5.7. Concatenate strings and numbers Python does not allow to concatenate a string directly with a number. It requires you to turn the number first into a string with the str() function. If you do not use str() you will get "TypeError: cannot concatenate 'str' and 'int' objects". For example: print 'this is a text plus a number ' + str(10) 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 forforfor 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)) BACK TO TOP 5.9.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    11 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us