30 Days Of Python 👨‍💻 - Day 16 - Module Basics

This article is a part of a 30 day Python challenge series. You can find the links to all the previous posts of this series here,
Today I spent some time exploring modules in Python. Structuring and organizing code is a very important aspect of development. As of now, we have structuring code in Python by using named functions and creating classes to organize functionality. However, when the size of the project increases, it is quite difficult to keep all code in a single file as it grows pretty big in size to read and understand the functionality. This problem is solved by using modules. A module in Python is a Python file (with .py extension) that contains some Python code. Using modules, a single file can be split into multiple files or modules based on functionality or features. Modules are a great way to organize and reuse code. Modules can then be used inside another module or the interactive Python interpreter using the import keyword.
 
Suppose we have two files in our project. main.py and utilities.py. main.py is the file that will be run by the interpreter.
 
The utilities.py shall have a function.
  1. def tagify(content, tag):  
  2.     return f'<{tag}>{content}</{tag}>'  
  3.   
  4.   
  5. def emojifier(content, emoji):  
  6.     return f'{emoji} {content} {emoji}'  
This function can then be used in the main.py file like this
  1. import utilities    
  2. result = utilities.tagify('hello world''p')    
  3. emoji_result = utilities.emojifier('python''😍')    
  4. print(result)  # <p>hello world</p>    
  5. print(emoji_result)  # 😍 python 😍     
Thus these utility functions can be imported and reused in any file hence improving code organization.
 

Ways of importing

 
There are different ways in which the import syntax can be used for importing modules
 
Import with renaming
  1. import utilities as utils    
  2. result = utils.tagify('hello world''p')    
  3. emoji_result = utils.emojifier('python''😍')    
  4. print(result)  # <p>hello world</p>    
  5. print(emoji_result)  # 😍 python 😍    
from… import statement
 
To import specific names from a module without importing the entire module
  1. from utilities import tagify, emojifier    
  2. result = tagify('hello world''p')    
  3. emoji_result = emojifier('python''😍')    
  4. print(result)  # <p>hello world</p>    
  5. print(emoji_result)  # 😍 python 😍   
Importing all names using *
  1. from utilities import *  
  2. result = tagify('hello world''p')  
  3.   
  4. emoji_result = emojifier('python''😍')  
  5.   
  6. print(result)  # <p>hello world</p>  
  7. print(emoji_result)  # 😍 python 😍  
The above way of importing is not considered as good programming practice as it impacts readability and can lead to duplicate definitions for an identifier.
 
__name__
 
There is a special dunder attribute__name__ in Python to check the name of the module.
  1. # utilities.py  
  2. print(__name__)  # utilities    
  3. def tagify(content, tag):  
  4.     return f'<{tag}>{content}</{tag}>'  
  5. def emojifier(content, emoji):  
  6.     return f'{emoji} {content} {emoji}'  
Note
The __name__ returns __main__ for the file which is run by the interpreter. Here coincidentally the file name is also main. However, the filename can be anything. There is a common practice in Python to execute a block of code based on the condition of whether the module is the main module or not.
  1. # main.py  
  2. from utilities import tagify, emojifier  
  3.   
  4. print(__name__)  # __main__  
  5. result = tagify('hello world''p')  
  6.   
  7. emoji_result = emojifier('python''😍')  
  8.   
  9. print(result)  # <p>hello world</p>  
  10. print(emoji_result)  # 😍 python 😍  
Packages
 
A package is simply a group of modules placed in a folder. Packages are used to group together modules with similar functionality just like how we keep all music files in a music folder, all videos in a videos folder to better organize files. Every Python module needs to contain a __init.py__ file. This lets the Python interpreter know that the directory is a Python package.
 
Let’s place the utilities module inside a directory named helper. This helper directory needs to have a __init.py__ file to make it a package. This package can then be imported in the main file as
  1. # main.py  
  2. from utilities import tagify, emojifier  
  3.   
  4. print(__name__)  
  5. result = tagify('hello world''p')  
  6.   
  7. emoji_result = emojifier('python''😍')  
  8.   
  9. print(result)  # <p>hello world</p>  
  10. print(emoji_result)  # 😍 python 😍  
  11.   
  12. if (__name__ == '__main__'):  
  13.     print('This is the main module'  
  14.           )  # This line gets printed as its the main module  
The above import can be made more convenient like this
  1. # main.py  
  2. import helper.utilities  
  3.   
  4. result = helper.utilities.tagify('hello world''p')  
  5.   
  6. emoji_result = helper.utilities.emojifier('python''😍')  
  7.   
  8. print(result)  # <p>hello world</p>  
  9. print(emoji_result)  # 😍 python 😍  
  10.   
  11. if (__name__ == '__main__'):  
  12.     print('This is the main module')  
Built-in modules
 
Python comes with a lot of built-in modules. These modules are downloaded along with the Python interpreter while installing Python. In some other programming languages, these are also known as standard libraries. These modules are developed and maintained by the Python core team and provide the out of the box functionality to do lot of cool things such as reading files, manipulate audio data, manipulating email and much more. List of all Python standard modules
 
These modules can be imported just like any other user-created module.
 
Let’s use a built-in module time to create a higher-order function that estimates the time it takes to run a function.
  1. import time  # built-in Python module    
  2. def list_maker(max_items):  
  3.     result = []  
  4.     for item in range(max_items):  
  5.         result.append(item)  
  6.     return result  
  7. def higher_order(func):  
  8.     def wrapper(item):  
  9.         time_start = time.time()  
  10.         list_maker(item)  
  11.         time_end = time.time()  
  12.         time_diff = time_end - time_start  
  13.         print(f'took {time_diff} seconds')  
  14.   
  15.     return wrapper  
  16. res = higher_order(list_maker)  
  17. print(res(100000))   
Note
The result will vary based on the system configuration.
 
In the functional programming section, we earlier also used a built-in module functools to import reduce function.
 
That’s all for today. Tomorrow we will explore the remaining concepts related to modules in Python such as using external Python packages and more.
 
Have a great one!


Similar Articles