Python/C4/Using-python-modules/English

From Script | Spoken-Tutorial
Jump to: navigation, search
Visual Cue Narration
Show Slide 1

Containing title, name of the production team along with the logo of MHRD

Hello Friends and Welcome to the spoken tutorial on 'Using Python Modules'.
Show Slide 2

Learning objectives

At the end of this tutorial, you will be able to ,
  1. Execute python scripts from command line.
  2. Use import in scripts.
  3. Import scipy and pylab modules.
  4. Use python standard modules and 3rd party modules.


Show Slide 3

Pre-requisite slide

Before beginning this tutorial,we would suggest you to complete the tutorial on "Using plot interactively", "Embellishing a plot" and "Saving plots".
Show Slide 4

On running python scripts from command line

Let us create a simple python script to print hello world. Open your text editor and type the following,
Open the text editor and type the following
print "Hello world!"
print
Save the script as hello.py Now save this script as hello.py,
Open the terminal
ipython
Start the ipython interpreter
 %run -i hello.py In the previous tutorials,we have seen how to run a script using the IPython interpreter using %run
Close the terminal but this is not the correct way of running a python script.
Open terminal and navigate to directory where hello.py was saved The correct method is to run it using the Python interpreter. Open the terminal and navigate to the directory where hello.py is,
python hello.py now run the Python script as,
It executed the script and we got the output Hello World!.
Show Slide 5

Highlight python filename syntax on slide while narrating

The syntax is python space filename.
Show Slide 6

Four plot problem

Now, we have a four plot problem where we have plotted four plots in a single figure. Let us run that script from command line.
Open the four_plot.py file in text editor and show Pause for some time and then continue If you don't have the script, then make one with the following set of commands
python four_plot.py Now let us run four_plot.py as a python script.
Oops! even though it was supposed to work, it didn't. It gave an error linspace() is not defined, which means that the function linspace() is not available in the current name-space.

But if you try to run the same script using %run -i four_plot.py in your IPython interpreter started with the option -pylab it will work, because the -pylab option does some work for us by importing the required modules to our name-space when ipython interpreter starts. And thus we don't have to explicitly import modules.

So now let us try to fix the problem and run the script in command line,

Show Slide 7

fix linspace problem

add this line as the first line in the script,
Add the line as first line in four_plot.py and save
from scipy import *
python four_plot.py Now let us run the script again,
Now it gave another error -- plot not defined,
Show Slide 8


let us edit the file again and add this line as the second line in our script and save it,
add the line as second line in four_plot.py and save
from pylab import *
Switch to the terminal
python four_plot.py
And now, run the script,
Yes! it worked. So what did we do?

We actually imported the required modules using the keyword import. It could also be done as by using,

Show Slide 9

Better way of fixing highlight the required line while narrating

from scipy import linspace

instead of,

from scipy import *

So in practice it is always good to use function names instead of asterisk or star. If we use asterisk to import from a particular module then it will replace any existing functions with the same name in our name-space.

Show Slide 10

Instead of *

So let us modify four_plot.py as, Hence we delete the first two lines of our code which we had added and add these lines
Switch to script 'four_plot.py' delete the first two lines and add the following
from scipy import linspace, pi, sin
from pylab import plot, legend, annotate
from pylab import xlim, ylim, title, show
python four_plot.py Now let us try running the code again as,
It works! In this method we actually imported the functions to the current name-space. There is one more way of doing it. And that is,
Show Slide 11

'another fix' highlight the required line while narrating

Notice that we use scipy.pi instead of just pi as in the previous method, and the functions are called as pylab.plot() and pylab.annotate() and not as plot() and annotate().

Pause the video here, try out the following exercise and resume the video.

Show Slide 12

Assignment 1

Write a script to plot a sine wave from minus two pi to two pi. <Pause> It can solved as,
Open sine.py and show it The first line we import the required functions linspace() , sin() and constant pi from the module scipy. The second and third line we import the functions plot(), legend(), show(), title(), xlabel() and ylabel(). And the rest the code to generate the plot.
Pause for sometime and then continue
Switch to the terminal
python sine.py
We can run it as,
As we can see, we our sine plot. Let us move further in our topic.
Show Slide 13

What is a module?

Until now we have been learning about importing modules, now what is a module?

A module is simply a file containing Python definitions and statements. Definitions from a module can be imported into other modules or into the main module.

Show Slide 14

Python standard library

Python has a very rich standard library of modules. It is very extensive, offering a wide range of facilities. Some of the standard modules are,

for Math: math, random for Internet access: urllib2, smtplib for System, Command line arguments: sys for Operating system interface: os for regular expressions: re for compression: gzip, zipfile, tarfile And there are lot more.

Find more information at Python Library reference, http://docs.python.org/library/

There are a lot of other modules like pylab, scipy, Mayavi, etc which are not part of the standard python library.

Show Slide 15

Summary

This brings us to the end of this tutorial. In this tutorial, we have learnt to,
  1. Run scripts from command line,
  2. Import modules by specifying the module name followed by an asterisk.
  3. Import only the required functions from modules by specifying the function name.
  4. Use python standard library.


Show Slide 16

Self assessment questions slide

Here are some self assessment questions for you to solve
  1. Which among this is correct ?
    • from scipy import plot
    • from numpy import plot
    • from matplotlib import plot
    • from pylab import plot
  1. Which among these libraries is part of python standard library ?
    • Mayavi
    • scipy
    • matplotlib
    • urllib2
  1. Functions xlim() and ylim() can be imported to the current name-space as,
    • from pylab import xlim, ylim
    • import pylab
    • from scipy import xlim, ylim
    • import scipy


Show Slide 17

Solution of self assessment questions on slide

And the answers,
  1. The option from pylab import plot is the correct one, since plot is a function of module module.
  2. urllib2 is a part of the python standard library.
  3. Functions xlim() and ylim() can be imported to the current name-space as, from pylab import xlim, ylim.


Show Slide 18

Acknowledgment slide

Hope you have enjoyed this tutorial and found it useful. Thank you!

Contributors and Content Editors

Chandrika