<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="https://script.spoken-tutorial.org/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://script.spoken-tutorial.org/index.php?action=history&amp;feed=atom&amp;title=Python%2FC4%2FUsing-python-modules%2FEnglish</id>
		<title>Python/C4/Using-python-modules/English - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://script.spoken-tutorial.org/index.php?action=history&amp;feed=atom&amp;title=Python%2FC4%2FUsing-python-modules%2FEnglish"/>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=Python/C4/Using-python-modules/English&amp;action=history"/>
		<updated>2026-05-14T13:48:52Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.23.17</generator>

	<entry>
		<id>https://script.spoken-tutorial.org/index.php?title=Python/C4/Using-python-modules/English&amp;diff=511&amp;oldid=prev</id>
		<title>Chandrika: Created page with '{| border=1 !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 tutori…'</title>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=Python/C4/Using-python-modules/English&amp;diff=511&amp;oldid=prev"/>
				<updated>2012-11-29T06:22:05Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;#039;{| border=1 !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 tutori…&amp;#039;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{| border=1&lt;br /&gt;
!Visual Cue&lt;br /&gt;
!Narration&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 1 &lt;br /&gt;
&lt;br /&gt;
Containing title, name of the production team along with the logo of MHRD &lt;br /&gt;
| Hello Friends and Welcome to the spoken tutorial on 'Using Python Modules'.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 2 &lt;br /&gt;
&lt;br /&gt;
Learning objectives &lt;br /&gt;
| At the end of this tutorial, you will be able to ,&lt;br /&gt;
&lt;br /&gt;
# Execute python scripts from command line.&lt;br /&gt;
# Use import in scripts.&lt;br /&gt;
# Import scipy and pylab modules.&lt;br /&gt;
# Use python standard modules and 3rd party modules.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 3 &lt;br /&gt;
&lt;br /&gt;
Pre-requisite slide &lt;br /&gt;
| Before beginning this tutorial,we would suggest you to complete the tutorial on &amp;quot;Using plot interactively&amp;quot;, &amp;quot;Embellishing a plot&amp;quot; and &amp;quot;Saving plots&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 4 &lt;br /&gt;
&lt;br /&gt;
On running python scripts from command line &lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| Let us create a simple python script to print hello world. Open your text editor and type the following,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Open the text editor and type the following &lt;br /&gt;
&lt;br /&gt;
 print &amp;quot;Hello world!&amp;quot;&lt;br /&gt;
 print&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Save the script as hello.py &lt;br /&gt;
| Now save this script as &amp;lt;tt&amp;gt;hello.py&amp;lt;/tt&amp;gt;,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Open the terminal &lt;br /&gt;
&lt;br /&gt;
 ipython&lt;br /&gt;
| Start the ipython interpreter&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  %run -i hello.py&lt;br /&gt;
| In the previous tutorials,we have seen how to run a script using the IPython interpreter using &amp;lt;tt&amp;gt;%run&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Close the terminal &lt;br /&gt;
| but this is not the correct way of running a python script.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Open terminal and navigate to directory where hello.py was saved &lt;br /&gt;
| The correct method is to run it using the Python interpreter. Open the terminal and navigate to the directory where hello.py is,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  python hello.py&lt;br /&gt;
| now run the Python script as,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| It executed the script and we got the output &amp;lt;tt&amp;gt;Hello World!&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 5 &lt;br /&gt;
&lt;br /&gt;
Highlight &amp;lt;tt&amp;gt;python filename&amp;lt;/tt&amp;gt; syntax on slide while narrating &lt;br /&gt;
| The syntax is &amp;lt;tt&amp;gt;python space filename&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 6 &lt;br /&gt;
&lt;br /&gt;
Four plot problem &lt;br /&gt;
| 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.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Open the four_plot.py file in text editor and show Pause for some time and then continue &lt;br /&gt;
| If you don't have the script, then make one with the following set of commands&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  python four_plot.py&lt;br /&gt;
| Now let us run four_plot.py as a python script.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| Oops! even though it was supposed to work, it didn't. It gave an error &amp;lt;tt&amp;gt;linspace()&amp;lt;/tt&amp;gt; is not defined, which means that the function &amp;lt;tt&amp;gt;linspace()&amp;lt;/tt&amp;gt; is not available in the current name-space.&lt;br /&gt;
&lt;br /&gt;
But if you try to run the same script using &amp;lt;tt&amp;gt;%run -i four_plot.py&amp;lt;/tt&amp;gt; in your IPython interpreter started with the option &amp;lt;tt&amp;gt;-pylab&amp;lt;/tt&amp;gt; it will work, because the &amp;lt;tt&amp;gt;-pylab&amp;lt;/tt&amp;gt; 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.&lt;br /&gt;
&lt;br /&gt;
So now let us try to fix the problem and run the script in command line,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 7 &lt;br /&gt;
&lt;br /&gt;
fix &amp;lt;tt&amp;gt;linspace&amp;lt;/tt&amp;gt; problem &lt;br /&gt;
| add this line as the first line in the script,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Add the line as first line in four_plot.py and save &lt;br /&gt;
&lt;br /&gt;
 from scipy import *&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  python four_plot.py&lt;br /&gt;
| Now let us run the script again,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| Now it gave another error -- plot not defined,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 8 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
| let us edit the file again and add this line as the second line in our script and save it,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| add the line as second line in four_plot.py and save &lt;br /&gt;
&lt;br /&gt;
 from pylab import *&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Switch to the terminal &lt;br /&gt;
&lt;br /&gt;
 python four_plot.py&lt;br /&gt;
| And now, run the script,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| Yes! it worked. So what did we do?&lt;br /&gt;
&lt;br /&gt;
We actually imported the required modules using the keyword &amp;lt;tt&amp;gt;import&amp;lt;/tt&amp;gt;. It could also be done as by using,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 9 &lt;br /&gt;
&lt;br /&gt;
Better way of fixing highlight the required line while narrating &lt;br /&gt;
| from scipy import linspace&lt;br /&gt;
&lt;br /&gt;
instead of,&lt;br /&gt;
&lt;br /&gt;
from scipy import *&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 10 &lt;br /&gt;
&lt;br /&gt;
Instead of &amp;lt;tt&amp;gt;&amp;lt;nowiki&amp;gt;*&amp;lt;/nowiki&amp;gt;&amp;lt;/tt&amp;gt; &lt;br /&gt;
| 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&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Switch to script 'four_plot.py' delete the first two lines and add the following &lt;br /&gt;
&lt;br /&gt;
 from scipy import linspace, pi, sin&lt;br /&gt;
 from pylab import plot, legend, annotate&lt;br /&gt;
 from pylab import xlim, ylim, title, show&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  python four_plot.py&lt;br /&gt;
| Now let us try running the code again as,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| 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,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 11 &lt;br /&gt;
&lt;br /&gt;
'another fix' highlight the required line while narrating &lt;br /&gt;
| Notice that we use &amp;lt;tt&amp;gt;scipy.pi&amp;lt;/tt&amp;gt; instead of just &amp;lt;tt&amp;gt;pi&amp;lt;/tt&amp;gt; as in the previous method, and the functions are called as &amp;lt;tt&amp;gt;pylab.plot()&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;pylab.annotate()&amp;lt;/tt&amp;gt; and not as &amp;lt;tt&amp;gt;plot()&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;annotate()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Pause the video here, try out the following exercise and resume the video.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 12 &lt;br /&gt;
&lt;br /&gt;
Assignment 1 &lt;br /&gt;
| Write a script to plot a sine wave from minus two pi to two pi. &amp;lt;nowiki&amp;gt;&amp;lt;Pause&amp;gt; It can solved as,&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Open sine.py and show it &lt;br /&gt;
| The first line we import the required functions &amp;lt;tt&amp;gt;linspace()&amp;lt;/tt&amp;gt; , &amp;lt;tt&amp;gt;sin()&amp;lt;/tt&amp;gt; and constant &amp;lt;tt&amp;gt;pi&amp;lt;/tt&amp;gt; from the module scipy. The second and third line we import the functions &amp;lt;tt&amp;gt;plot()&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;legend()&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;show()&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;title()&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;xlabel()&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;ylabel()&amp;lt;/tt&amp;gt;. And the rest the code to generate the plot.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Pause for sometime and then continue &lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Switch to the terminal &lt;br /&gt;
&lt;br /&gt;
 python sine.py&lt;br /&gt;
| We can run it as,&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| As we can see, we our sine plot. Let us move further in our topic.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 13 &lt;br /&gt;
&lt;br /&gt;
What is a module? &lt;br /&gt;
| Until now we have been learning about importing modules, now what is a module?&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 14 &lt;br /&gt;
&lt;br /&gt;
Python standard library &lt;br /&gt;
| 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,&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Find more information at Python Library reference, &amp;lt;tt&amp;gt;http://docs.python.org/library/&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are a lot of other modules like pylab, scipy, Mayavi, etc which are not part of the standard python library.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 15&lt;br /&gt;
&lt;br /&gt;
Summary &lt;br /&gt;
| This brings us to the end of this tutorial. In this tutorial, we have learnt to,&lt;br /&gt;
&lt;br /&gt;
# Run scripts from command line,&lt;br /&gt;
# Import modules by specifying the module name followed by an asterisk.&lt;br /&gt;
# Import only the required functions from modules by specifying the function name.&lt;br /&gt;
# Use python standard library.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 16 &lt;br /&gt;
&lt;br /&gt;
Self assessment questions slide &lt;br /&gt;
| Here are some self assessment questions for you to solve&lt;br /&gt;
&lt;br /&gt;
# Which among this is correct ?&lt;br /&gt;
** from scipy import plot&lt;br /&gt;
** from numpy import plot&lt;br /&gt;
** from matplotlib import plot&lt;br /&gt;
** from pylab import plot&lt;br /&gt;
# Which among these libraries is part of python standard library ?&lt;br /&gt;
** Mayavi&lt;br /&gt;
** scipy&lt;br /&gt;
** matplotlib&lt;br /&gt;
** urllib2&lt;br /&gt;
# Functions &amp;lt;tt&amp;gt;xlim()&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;ylim()&amp;lt;/tt&amp;gt; can be imported to the current name-space as,&lt;br /&gt;
** from pylab import xlim, ylim&lt;br /&gt;
** import pylab&lt;br /&gt;
** from scipy import xlim, ylim&lt;br /&gt;
** import scipy&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 17&lt;br /&gt;
&lt;br /&gt;
Solution of self assessment questions on slide &lt;br /&gt;
| And the answers,&lt;br /&gt;
&lt;br /&gt;
# The option &amp;lt;tt&amp;gt;from pylab import plot&amp;lt;/tt&amp;gt; is the correct one, since plot is a function of module module.&lt;br /&gt;
# &amp;lt;tt&amp;gt;urllib2&amp;lt;/tt&amp;gt; is a part of the python standard library.&lt;br /&gt;
# Functions &amp;lt;tt&amp;gt;xlim()&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;ylim()&amp;lt;/tt&amp;gt; can be imported to the current name-space as, &amp;lt;tt&amp;gt;from pylab import xlim, ylim&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 18&lt;br /&gt;
&lt;br /&gt;
Acknowledgment slide&lt;br /&gt;
| Hope you have enjoyed this tutorial and found it useful. Thank you!&lt;br /&gt;
&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Chandrika</name></author>	</entry>

	</feed>