My Blog
Articles related to programming, computer science, technology and research.

19/04/2018 Categories: Python Programming. No Comments on Modules in Python Programming



A function allows to reuse a piece of code. A module on the other hand contains multiple functions, variables, and other elements which can be reused. A module is a Python file with .py extension. Each .py file can be treated as a module.


We can print the name of existing module by using the __name__ attribute of the module as follows:



Above code prints the name of module as __main__. By default the name of the current module will be __main__.


Module Loading and Execution


A module imported in a Python program should available where the program file is located. If the module is not available where the program file is, it looks for the module in the directories available in PYTHONPATH. If the module is still not found, it will search in the lib directory of Python installation. If still not found, it will generated error names ImportError.


Once a module is located, it is loaded into memory. A compiled version of the module will be created with .pyc extension. When the module is referenced next time, the .pyc file will be loaded into memory instead of recompiling it. A new compiled version (.pyc) will be generated whenever it is out of date. Programmer can also force the Python shell to recompile the .py file by using the reload function.


import Statement


The import keyword allows to use functions or variables available in a module. Syntax of import statement is as follows:



After importing the module, we can access functions and variables available in it as follows:



Following program imports the pre-defined sys module and prints the PYTHONPATH information by using the path variable as follows:



Following program imports the pre-defined random module and prints a random number by calling the choice function as follows:



Above code generates a random number in the range 1 to 100.


from…import Statement


A module contains several variables and functions. When we use import statement we are importing everything in the module. To import only selected variables or functions from the module, we use from…import statement. To import everything from the module we can write:



For example to import the pi variable from the math module we can write:



Now, we can directly use pi variable in our program as:



We can import multiple elements from the module as:



We can also import a module element with another name (alias) using the as keyword as follows:



We can access the command line parameters passed to a Python script using the argv variable available in the sys module as follows:



We can terminate a Python script abruptly by using the exit() method as follows:



Creating Modules


A python file with .py extension is treated as a module. For example, the below script is saved as



Following is the main module file named in which we import our own module circle as follows:



Notice that we are using area() function which was defined in the module circle. Remember that and files should be at the same location.


Steps for creating and using our own module:


  • Define the variables, functions, and other elements that you want and save the file as
  • The filename will server as the module name.
  • Now, create a new file (our main module) in which we can import our module by using import or from…import statement.
  • Use the variables or functions in the created module using dot (.) operator.




A namespace is a logical collection of names. Namespaces are used to eliminate name collisions. Python does not allow programmers to create multiple identifiers with same name. But, in some case we need identifiers having same name.


For example, consider two modules module1 and module have the same function foo() as follows:



When we import both modules into same program and try to refer foo(), error will be generated:



To avoid the error, we have to use fully qualified name as follows:



Each module has its own namespace.


Built-in, Global, Local Namespaces


The built-in namespace contains all the built-in functions and constants from the __builtin__ library. The global namespace contains all the elements in the current module. The local namespace contains all the elements in the current function or block. Runtime will search for the identifier first in the local, then in the global, then finally in the built-in namespace.

Related Links:

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll Up