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,
- 30 Days of Python 👨💻 - Day One - Introduction
- 30 Days Of Python 👨💻 - Day 2 - Data Types I
- 30 Days of Python 👨💻 - Day 3 - Data Types II
- 30 Days Of Python 👨💻 - Day 4 - Data Types III
- 30 Days Of Python 👨💻 - Day 5 - Conditions And Loops I
- 30 Days Of Python 👨💻 - Day 6 - Loops II And Functions
- 30 Days Of Python 👨💻 - Day 7 - Developer Environment
- 30 Days Of Python 👨💻 - Day 8 - OOP Basics
- 30 Days Of Python 👨💻 - Day 9 - OOP Pillars
- 30 Days Of Python 👨💻 - Day 10 - OOP Missing Pieces
- 30 Days Of Python 👨💻 - Day 11 - Functional Programming
- 30 Days Of Python 👨💻 - Day 12 - Lambda Expression And Comprehensions
- 30 Days Of Python 👨💻 - Day 13 - Decorators
- 30 Days Of Python 👨💻 - Day 14 - Handling Errors
- 30 Days Of Python 👨💻 - Day 15 - Generators
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.
- def tagify(content, tag):
- return f'<{tag}>{content}</{tag}>'
- def emojifier(content, emoji):
- return f'{emoji} {content} {emoji}'
This function can then be used in the main.py file like this
- import utilities
- result = utilities.tagify('hello world', 'p')
- emoji_result = utilities.emojifier('python', '😍')
- print(result) # <p>hello world</p>
- 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
- import utilities as utils
- result = utils.tagify('hello world', 'p')
- emoji_result = utils.emojifier('python', '😍')
- print(result) # <p>hello world</p>
- print(emoji_result) # 😍 python 😍
from… import statement
To import specific names from a module without importing the entire module
- from utilities import tagify, emojifier
- result = tagify('hello world', 'p')
- emoji_result = emojifier('python', '😍')
- print(result) # <p>hello world</p>
- print(emoji_result) # 😍 python 😍
Importing all names using *

Join the conversation! Your thoughts help the community grow.