Python Development With PyDev and Eclipse

download Python Development With PyDev and Eclipse

of 12

Transcript of Python Development With PyDev and Eclipse

  • 8/10/2019 Python Development With PyDev and Eclipse

    1/12

    Python Development with PyDev and Eclipse - Tutorial

    1. Overview

    1.1. What is Python

    Pythonis an interpreted programming language and claims to be a very effective programming

    language.Pythonwas develop by Guido van Rossum.

    The name Python is based on the TV show calledMonty Python's Flying Circus. During execution

    the Python source code is translated into bytecode which is then interpreted by the Python

    interpreter.Pythonsource code can also run on the Java Virtual achine! in this case you are using

    Jython.

    "ey features ofPythonare#

    high$level data types! as for example extensible lists

    statement grouping is done by indentation instead of brac%ets

    variable or argument declaration is not necessary

    supports for ob&ect$orientated! procedural and functional programming style

    1.2. Block concept in Pythonvia indentation

    Pythonidentify bloc%s of code by indentation. 'f you have an if statement and the next line isindented then it means that this indented bloc% 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 ) spaces per

    indentation level.

    1.3. About this tutorial

    This tutorial will first explain how to install Python and the Python plugins for *clipse. 't will then

    create a small Python pro&ect to show the usage of the plugin. +fterwards the general constructs of

    Python are explained.

    2. Installation

    2.1. Python

    Download Python from http#,,www.python.org. Download the version -.-. or higher of Python. 'f

    you are using /indows you can use the native installer for Python.

    2.2. Eclipse Python plugin

    The following assume that you have already *clipseinstalled. 0or an installation description of

    *clipse please see *clipse 'D* for Java.

    http://www.python.org/http://www.vogella.com/tutorials/Eclipse/article.htmlhttp://www.vogella.com/tutorials/Eclipse/article.htmlhttp://www.python.org/http://www.vogella.com/tutorials/Eclipse/article.htmlhttp://www.vogella.com/tutorials/Eclipse/article.html
  • 8/10/2019 Python Development With PyDev and Eclipse

    2/12

    0or Python development under *clipse you can use the PyDev Plugin which is an open source

    pro&ect. 'nstall PyDev via the *clipse update manager via the following update site#

    http#,,pydev.org,updates

    2.3. Confguration o Eclipse

    1ou also have to maintain in *clipse the location of your Python installation. 2pen in the /indow

    3 Preference 3 Pydev 3 'nterpreter Python menu.

    Press the 4ew button and enter the path to python.exein your Python installation directory. 0or

    5inux and ac 267 users this is normally ,usr,bin,python.

    Warning

    4ote that on +8 267 the python libraries are not installed by default. Please use Google to find

    out! how to install them on a ac 267 system! the author of this text only uses 5inux.

    The result should loo% li%e the following.

  • 8/10/2019 Python Development With PyDev and Eclipse

    3/12

    3. Your irst Python pro!ram in Eclipse

    6elect 0ile $9 4ew $9 Pro&ect. 6elect Pydev $9 Pydev Pro&ect.

    8reate a new pro&ect with the name (de.vogella.python.first(. 6elect Python version :.; and your

    interpreter.

  • 8/10/2019 Python Development With PyDev and Eclipse

    4/12

    Press finish.

    6elect /indow$92pen Perspective $92ther. 6elect the PyDev perspective.

    6elect the (src( folder of your pro&ect! right$clic% it and select 4ew $9 PyDev odul. 8reate a

    module (0irstodule(.

  • 8/10/2019 Python Development With PyDev and Eclipse

    5/12

    8reate the following source code.

    '''Created on 18.06.2009

    @author:Lars Vogel'''def add(a,b) ret!rn a"b

    def add#$xedVal!e(a) y % & ret!rn y "apr$nt add(1,2)pr$nt add#$xedVal!e(1)

    Right$clic% your model and select Run +s $9 Python run.

  • 8/10/2019 Python Development With PyDev and Eclipse

    6/12

    8ongratulations< 1ou created your first =little> Python modul and ran it function.

    'f you do not use str=> you will get (Type*rror# cannot concatenate KstrK and KintK ob&ects(.

    0or example#

    pr$nt 'th$s $s a text pl!s a n!*ber '" str(10)

  • 8/10/2019 Python Development With PyDev and Eclipse

    10/12

  • 8/10/2019 Python Development With PyDev and Eclipse

    11/12

    o!tp!t.7r$te(l$ne.rstr$p() " 'n')f.lose()

    !.1. *plitting strings and co"paring lists.

    The following example is contained in the pro&ect (de.vogella.python.files(. 't reads to files which

    contain one long comma separated string. This string is splitted into lists and the lists are compared.

    f1 % open('te*pla!nhonf$g1.txt', 'r')s% ++for l$ne $n f1 s"%l$nef1.lose()

    f2 % open('te*pla!nhonf$g2.txt', 'r')s2% ++for l$ne $n f2 s2"%l$ne

    f2.lose()l$st1 % s.spl$t(+,+)l$st2 % s2.spl$t(+,+)?pr$nt(len(l$st1))pr$nt(len(l$st2))

    d$fferene % l$st(set(l$st1).d$fferene(set(l$st2)))

    pr$nt (d$fferene)

    !.11. Writing Python *cripts in /nicode

    'f you read special non +68'' sign! e.g. L! M. N or O! you have to tell Python which character set touse. 'nclude the following in the first or second line of your script.

    ;@; od$ng A#;8 ;@;

    !.12. Classes in Python

    The following is a defined class in Python. Python uses the naming convension name for

    internal functions.

    Python allows operator overloading! e.g. you can define what the operator H will to for a specific

    class.

    Ta#le 3.

    ''init'' %onstructor o the class

    str The method which is called if print is applied to this ob&ect

    add H 2perator

    mul Q 2perator

  • 8/10/2019 Python Development With PyDev and Eclipse

    12/12

    The empty ob&ect =null> is calledNonein Python.

    lass o$nt

    def >>$n$t>>(self, x%0, y%0) self.x % x self.y % y def >>str>>(self) ret!rn +x;:al!e+" str(self.x) " + y;:al!e+" str(self.y) def >>add>>(self,other) p % o$nt() p.x % self.x"other.x p.y % self.y"other.y ret!rn pp1 % o$nt(B,)p2 % o$nt(2,B)pr$nt p1pr$nt p1.ypr$nt (p1"p2)

    (. )oo!le *pp En!ine

    Google offers free hosting of small Python based web application. Please see Google +pp *ngine

    development with Python.

    +. ,upport this we#site

    This tutorial is 2pen 8ontent under the 88 1$48$6+ -.F D*license. 6ource code in this tutorial

    is distributed under the *clipse Public 5icense.6ee the vogella 5icensepage for details on the terms

    of reuse.

    /riting and updating these tutorials is a lot of wor%. 'f this free community service was helpful! you

    can support the cause by giving a tip as well as reporting typos and factual errors.

    +.1. 0hank you

    Please consider a contribution if this article helped you.

    http://www.vogella.com/tutorials/GoogleAppEngine/article.htmlhttp://www.vogella.com/tutorials/GoogleAppEngine/article.htmlhttp://creativecommons.org/licenses/by-nc-sa/3.0/de/deed.enhttps://www.eclipse.org/legal/epl-v10.htmlhttps://www.eclipse.org/legal/epl-v10.htmlhttp://www.vogella.com/license.htmlhttp://www.vogella.com/tutorials/GoogleAppEngine/article.htmlhttp://www.vogella.com/tutorials/GoogleAppEngine/article.htmlhttp://creativecommons.org/licenses/by-nc-sa/3.0/de/deed.enhttps://www.eclipse.org/legal/epl-v10.htmlhttp://www.vogella.com/license.html