Web Programming with Python: Modules II

In the previous tutorial the main focus was on how to acquire the modules. In this tutorial you will learn more about modules and how to interact with them. While dealing with modules it is important to know where it goes when it is downloaded. Once you are in the Python directory (we have learnt about that in the previous tutorials), you will find a folder called lib. All the standard library modules are in this lib folder. All modules that came with the installation of Python can be found here. There are third party modules as well which are installed into “site packages” inside the “lib” folder. When you click on “site packages” folder you will see modules that have been personally installed.

When you import a module in your program it works as if all the lines of code in the module are written in your script. Modules are full of Python code and to know more about what a module is all about, you must go through its code. Most developers heavily comment in modules so that they can provide better understanding about the code. When you import a module, Python is going to look for it at different locations. The first location will be where your script is, the second location is the “lib” folder and third location is the “site packages”.

So, the primary location where the Python is going to look for the code is local, ie. ,where your script is installed. Now to create a local module, you have to create a python file (in our example we stored the file with the name sample_module.py) in the local directory where your code is installed. To, get a better understanding let’s create a small sample module which will have the following code in it:

def output(text):
    Print text

So, the ‘output’ function in the module basically takes text as input and prints it. Now open your main python program file and import the module as shown below:

import sample_module

Now that you have imported this module you can reference things within it. So, let’s try to call the output function from our program as follows:

sample_module.output('Hello')

So, when you execute the program ”Hello” will be printed on the shell.

Now, another way to call the output function is:

import output from sample_module;
output(‘Hello’)

The output for this will also be the same. However, we use this statement when we are sure that we just need to call one function from the module.

Now, another interesting thing is to rename a function of a module in your script

import output from sample_module as o;
o(‘Hello’)

This will give the same result as well.

Suppose there are several functions in your module then you can use a * to import everything as follows:

from sample_module import  *
output(‘Hello’)

So, even this piece of code will have the same result.

In the end there is just one small advice regarding naming your modules. While creating a module be careful about naming them in order to avoid any sort of conflicts. As mentioned above, when you import a module, Python first looks for it in the local directory and if you already have some other old python file with the same name in that directory then it may happen that your program will import the wrong code. So, before naming your module make sure that no file of that name exists in the local, lib or sire packages folder.

With this we come to the end of this tutorial and this section of ‘Python Programming Review’ as well.

Online Technology Courses Stone River eLearning

GET YOUR FREE PYTHON EBOOK!

Comments