Python Modules

A file containing Python definitions and statements is called a Python Module. A Module can have functions, classes, variables, and runnable code. A Module can be considered similar to a code library. It is a file containing a set of functions that we want to include in our functions, just like we use different libraries like string.h, math.h, vector, queue, set, etc. in C/C++. 

Adding related code in one file and grouping it makes it easier to use and understand, and we don’t need to add the same code, again and again, every time, whenever we use them. It makes the code more logically organized and readable. 

Creating a Simple Module – 

We can create a Python Module by using the .py extension for a Module File –


def print_name(name): 
print(“My Name is “ + name) 

So this is how you can create simple to more complex module files according to your use and then reuse them in your codes by just importing these files into your main file.

How to use other modules in your code file – 

Using Import Module in Python – Import Statement 

We can use other modules in another code file by using the import statement in the source file.

Syntax –

import module_name

Whenever we use this import statement, the compiler looks for the module in the search directory(a list where the compiler looks for the required module file), and if the module is found, the features, variables, functions, etc. can be used in the source file, but if don’t find the module in the search directory, then an error is thrown. 

Now you have imported the module in the source file, but still, you directly cant access its data members and functions. 

To access them, we need to use the (.) dot operator with the module functions.

Example – 

Let’s suppose there is a module for the addition of two numbers – the name of the module is an addition 

def add(a, b): 
return (a+b)

Now we want to add this module to outsource file, so import addition # importing the addition module into the source file print(addition.add(10, 5)) # using the . operator to access the member functions of the module. Now, if we don’t want to import the whole module, but a small part of it or a small file from it. So we can use them as import statement

The from import Statement – The from import Statement allows importing only some specific functions, variables, or attributes of the module to the source file without importing the whole module.

Syntax –

from module_name import function_name 

Example – 

from math import sqrt #importing only sqrt function from the math module print(sqrt(25))

Import * statement from a module – 

Using the import * statement imports all the specific names and functions from a module.

Syntax – 

from module_name import * 

Variables in Modules – 

The module can take functions, variables, etc. It can take variables of all types like arrays, dictionaries, and objects.

Example –

human = { 
"name" : "Ram", 
"age" : "20", 
"nationality" : "Indian" 
} #you can create a module of this type with any name (module_human , here) and then can use it in another file 
#using the module in another file 
import module_human 
a = module_human.human["nationality"] 
print(a) #this will print the nationality of the person in the module 

Renaming a Module using the as Keyword – 

We can rename any module by using the as keyword. This helps to use a smaller name or alias name for the module. 

Syntax –

import module_previous_name as module_new_name 

Example – as we use the pandas library in python, if we want to use its short form, so we will use 

Import Pandas as pd #By writing this statement, we can use the pandas library as pd, and if we write pd in the code, it will be referred to as pandas. 

dir() Function – If we want to know the function names or variable names in a module, then Python provides a built-in function for that, i.e., dir().

Synatx –


Example –

import math 
a = dir(math) 

Special thanks to Ishita Dhiman for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article