using python modules/script.rst
changeset 522 d33698326409
parent 521 88a01948450d
child 523 54bdda4aefa5
equal deleted inserted replaced
521:88a01948450d 522:d33698326409
     1 .. Objectives
       
     2 .. ----------
       
     3 
       
     4 .. At the end of this tutorial, you will be able to 
       
     5 
       
     6 .. 1. Execute python scripts from command line.
       
     7 .. #. Use import in scripts.
       
     8 .. #. Import scipy and pylab modules
       
     9 .. #. Use python standard modules and 3rd party modules.
       
    10 
       
    11 
       
    12 .. Prerequisites
       
    13 .. -------------
       
    14 
       
    15 ..   1. should have ``pylab`` installed. 
       
    16 ..   #. using plot command interactively.
       
    17 ..   #. embellishing a plot.
       
    18 ..   #. saving plots.
       
    19      
       
    20 .. Author              : Anoop Jacob Thomas <anoop@fossee.in>
       
    21    Internal Reviewer   : Puneeth
       
    22    External Reviewer   :
       
    23    Language Reviewer   : Bhanukiran
       
    24    Checklist OK?       : <11-11-2010, Anand, OK> [2010-10-05]
       
    25 
       
    26 
       
    27 ====================
       
    28 Using Python modules
       
    29 ====================
       
    30 {{{ show the welcome slide }}}
       
    31 
       
    32 Welcome to the spoken tutorial on Using Python Modules.
       
    33 
       
    34 {{{ switch to next slide, outline slide }}}
       
    35 
       
    36 In this tutorial, we will see how to run python scripts from command
       
    37 line. We'll see how to import modules, importing scipy and pylab
       
    38 modules and have a look at the Python standard library.
       
    39 
       
    40 {{{ switch to next slide on executing python scripts from command line }}}
       
    41 
       
    42 Let us create a simple python script to print hello world. Open your
       
    43 text editor and type the following,
       
    44 
       
    45 {{{ open the text editor and type the following }}}
       
    46 ::
       
    47 
       
    48     print "Hello world!"
       
    49     print
       
    50 
       
    51 and save the script as ``hello.py``,
       
    52 
       
    53 {{{ save the script as hello.py }}}
       
    54 
       
    55 Till now we saw how to run a script using the IPython interpreter
       
    56 using the
       
    57 ::
       
    58 
       
    59     %run -i hello.py
       
    60 
       
    61 option, but that is not the correct way of running a python
       
    62 script. 
       
    63 
       
    64 The correct method is to run it using the Python interpreter. Open the
       
    65 terminal and navigate to the directory where hello.py is,
       
    66 
       
    67 {{{ open terminal and navigate to directory where hello.py was saved }}}
       
    68 
       
    69 {{{ switch to next slide }}}
       
    70 
       
    71 now run the Python script as,
       
    72 ::
       
    73 
       
    74     python hello.py
       
    75 
       
    76 It executed the script and we got the output ``Hello World!``.
       
    77 
       
    78 {{{ highlight ``python filename`` syntax on slide while narrating }}}
       
    79 
       
    80 The syntax is python space filename.
       
    81 
       
    82 {{{ switch to next slide, four plot problem }}}
       
    83 
       
    84 Now recall the four plot problem where we plotted four plots in a single
       
    85 figure. Let us run that script from command line.
       
    86 
       
    87 If you don't have the script, 
       
    88 
       
    89 {{{ open the four_plot.py file in text editor }}}
       
    90 
       
    91 just pause here and create a python script with the following lines
       
    92 and save it as four_plot.py.
       
    93 
       
    94 Now let us run four_plot.py as a python script.
       
    95 ::
       
    96 
       
    97     python four_plot.py
       
    98 
       
    99 Oops! even though it was supposed to work, it didn't. It gave an error
       
   100 ``linspace()`` is not defined, which means that the function
       
   101 ``linspace()`` is not available in the current name-space.
       
   102 
       
   103 But if you try to run the same script using ``%run -i four_plot.py``
       
   104 in your IPython interpreter started with the option ``-pylab`` it will
       
   105 work, because the ``-pylab`` option does some work for us by importing
       
   106 the required modules to our name-space when ipython interpreter
       
   107 starts. And thus we don't have to explicitly import modules.
       
   108 
       
   109 So now let us try to fix the problem and run the script in command
       
   110 line,
       
   111 
       
   112 {{{ switch to next slide, fix ``linspace`` problem }}}
       
   113 
       
   114 add the following line as the first line in the script,
       
   115 {{{ add the line as first line in four_plot.py and save }}}
       
   116 ::
       
   117 
       
   118     from scipy import *
       
   119 
       
   120 Now let us run the script again,
       
   121 ::
       
   122 
       
   123     python four_plot.py
       
   124 
       
   125 Now it gave another error -- plot not defined, let us edit the file
       
   126 again and add the line below the line we just added,
       
   127 
       
   128 {{{ switch to next slide, fix ``plot`` problem }}}
       
   129 
       
   130 {{{ add the line as second line in four_plot.py and save }}}
       
   131 ::
       
   132 
       
   133     from pylab import *
       
   134 
       
   135 And run the script,
       
   136 ::
       
   137 
       
   138     python four_plot.py
       
   139 
       
   140 Yes! it worked. So what did we do?
       
   141 
       
   142 We actually imported the required modules using the keyword ``import``.
       
   143 It could have also be done as,
       
   144 
       
   145 {{{ switch to next slide, better way of fixing }}}
       
   146 
       
   147 {{{ highlight the following in slide and say it loud }}}
       
   148 ::
       
   149 
       
   150     from scipy import linspace
       
   151 
       
   152 instead of,
       
   153 ::
       
   154 
       
   155     from scipy import *
       
   156 
       
   157 So in practice it is always good to use function names instead of
       
   158 asterisk or star. If we use asterisk to import from a particular
       
   159 module then it will replace any existing functions with the same name
       
   160 in our name-space.
       
   161 
       
   162 {{{ switch to next slide, Instead of ``*`` }}}
       
   163 
       
   164 So let us modify four_plot.py as,
       
   165 {{{ delete the first two lines and add the following }}}
       
   166 ::
       
   167 
       
   168     from scipy import linspace, pi, sin
       
   169     from pylab import plot, legend, annotate
       
   170     from pylab import xlim, ylim, title, show
       
   171 
       
   172 Now let us try running the code again as,
       
   173 ::
       
   174 
       
   175     python four_plot.py
       
   176 
       
   177 It works! In this method we actually imported the functions to the
       
   178 current name-space, and there is another method of doing it. And that
       
   179 is,
       
   180 
       
   181 {{{ switch to next slide }}}
       
   182 
       
   183 Notice that we use ``scipy.pi`` instead of just ``pi`` as in the
       
   184 previous method, and the functions are called as ``pylab.plot()`` and
       
   185 ``pylab.annotate()`` and not as ``plot()`` and ``annotate()``.
       
   186 
       
   187 {{{ switch to next slide, problem statement }}}
       
   188 
       
   189 %% %% Write a script to plot a sine wave from minus two pi to two pi.
       
   190 
       
   191 Pause here and try to solve the problem yourself before looking at the
       
   192 solution.
       
   193 
       
   194 It can solved as,
       
   195 
       
   196 {{{ open sine.py and show it }}}
       
   197 
       
   198 the first line we import the required functions ``linspace()`` and
       
   199 ``sin()`` and constant ``pi`` from the module scipy. the second and
       
   200 third line we import the functions ``plot()``, ``legend()``,
       
   201 ``show()``, ``title()``, ``xlabel()`` and ``ylabel()``. And the rest
       
   202 the code to generate the plot.
       
   203 
       
   204 We can run it as,
       
   205 {{{ now switch focus to terminal and run the script }}}
       
   206 ::
       
   207 
       
   208     python sine.py
       
   209 
       
   210 {{{ switch to next slide, What is a module? }}}
       
   211 
       
   212 Until now we have been learning about importing modules, now what is a
       
   213 module?
       
   214 
       
   215 A module is simply a file containing Python definitions and
       
   216 statements. Definitions from a module can be imported into other
       
   217 modules or into the main module.
       
   218 
       
   219 {{{ switch to next slide, Python standard library }}}
       
   220 
       
   221 Python has a very rich standard library of modules. It is very
       
   222 extensive, offering a wide range of facilities. Some of the standard
       
   223 modules are,
       
   224 
       
   225 for Math: math, random
       
   226 for Internet access: urllib2, smtplib
       
   227 for System, Command line arguments: sys
       
   228 for Operating system interface: os
       
   229 for regular expressions: re
       
   230 for compression: gzip, zipfile, tarfile
       
   231 And there are lot more.
       
   232 
       
   233 Find more information at Python Library reference,
       
   234 ``http://docs.python.org/library/``
       
   235 
       
   236 There are a lot of other modules like pylab, scipy, Mayavi, etc which
       
   237 are not part of the standard python library.
       
   238 
       
   239 {{{ switch to next slide, summary }}}
       
   240 
       
   241 This brings us to the end of this tutorial, in this tutorial we
       
   242 learned running scripts from command line, learned about modules, saw
       
   243 the python standard library.
       
   244 
       
   245 {{{ switch to next slide, thank you slide }}}
       
   246 
       
   247 Thank you!