From Script | Spoken-Tutorial
Jump to: navigation, search
Time Narration
00:01 Hello friends and welcome to the tutorial on "Writing Python scripts".
00:05 At the end of this tutorial, you will be able to,

Understand what is importing. Write your own Python modules. Understand the __name__ == in double quotes __main__ underscore idiom

00:19 Before beginning this tutorial,we would suggest you to complete the tutorial on "Using Python modules".
00:25 Often we will have to reuse the code that we have written.
00:29 We do that by writing functions.
00:31 Functions are bundled into packages and are imported as and when required in other scripts.
00:37 Let us first write a function that computes the gcd of two numbers and save it in a script.
00:44 Open an editor and type the code.
00:47 Please take care of the indentation.
00:51 So type in terminal gcd underscore then type def gcd within bracket a comma b colon while b colon a comma b = b comma a percentage b return a
01:49 We shall write a test function in the script that tests the gcd function every time the script is run.
01:56 So type

if gcd within bracket 40 comma 12 == 4 colon, print within double quotes Everything OK else colon print within double quotes The GCD function is wrong

02:53 Let us save the file as in slash home slash fossee slash gcd
03:05 We shall run the script by typing in the terminal
03:11 python slash home slash fossee slash gcd underscore
03:25 We can see that the script is executed and everything is fine.
03:29 What if we want to use the gcd function in some of our other scripts.
03:35 This is also possible since every python file can be used as a module.
03:39 But first, we shall understand what happens when you import a module.
03:43 So type ipython in terminal.
03:52 Open IPython and type import sys (hit enter). sys.path
04:15 This is a list of locations where python searches for a module when it encounters an import statement.
04:20 Hence, when we just did import sys , python searches for a file named or a folder named sys in all these locations one by one, until it finds one.
04:34 We can place our script in any one of these locations and can import it.
04:38 The first item in the list is an empty string which means the current working directory is also searched.
04:45 Alternatively, we can also import the module if we are working in same directory where the script exists.
04:53 Since we are in slash home slash fossee, we can simply do
04:59 By typing on the terminal import gcd underscore and hit enter.
05:12 We can see that the gcd underscore script is imported.
05:18 But the test code that we added at the end of the file is also executed.
05:23 But we want the test code to be executed only when the file is run as a python script or not when it is imported.
05:30 This is possible by using underscore underscore name underscore underscore variable.
05:34 First, we shall look at how to use the idiom and then understand how it works.
05:41 Go to the file and add this line as the beginning of the code and indent the code accordingly.
05:47 So type if underscore underscore name underscore underscore == within double quotes underscore underscore main underscore underscore colon
06:30 Let us first run the code.
06:33 So type in terminal python gcd underscore
06:45 We can see that
06:48 There is an error coming up, its showing indentation error.
06:57 So we have to edit the file and make indentation correct.
07:20 We can see that the test runs successfully.
07:24 Now we shall import the file gcd underscore script
07:29 So type import gcd underscore script
07:39 We see that now the test code is not executed.The underscore underscore name underscore underscore variable is local to every module and it is equal to underscore underscore main underscore underscore only when the file is run as a script.
07:54 Hence, all the code that goes in to the if block, if name == within double quotes main colon is executed only when the file is run as a python script.
08:08 This brings us to the end of the tutorial.
08:11 In this tutorial, we have learnt to, 1. Know what happens when we import a module.
08:16 2.Use a script as a module.
08:18 3. Write test functions using the name idiom.
08:22 Here are some self assessment questions for you to solve
08:26 Which of the following variables contains the locations to search for python modules

sys.pythonpath, sys.path, os.pythonpath, os.path

08:38 A module should contain only functions. - True - False
08:43 The script is in one of locations of PYTHONPATH and contains the following code and the code is given.
09:00 And now look at the answers,
09:04 sys.path contains the locations to search for python modules.
09:10 False is the answer.
09:13 A module which contain a range of functions.
09:19 After doing import utils , we can use the function show() as, within bracket double quotes hey
09:32 So we hope you have enjoyed this tutorial and found it useful.
09:35 Thank you!

Contributors and Content Editors

Gaurav, Minal, PoojaMoolya, Sneha