Modules and Packages
Introduction
Python, a versatile and powerful programming language, owes much of its success to its modular and extensible nature. In Python, code organization is achieved through the use of modules and packages. Modules and packages are fundamental concepts that contribute to the elegance and maintainability of Python code. These concepts facilitate better code management, reusability, and collaboration among developers. In this blog post, we will delve into the world of modules and packages, exploring their significance, understanding their roles, and benefits, and how they enhance code organization and reuse.

Sections
- What is mean by modules
- Create modules.
- Creating Python modules in Google Colab
- Importing module objects to the current namespace
- Importing all objects from a module
- Custom import name
- Module initialization
- Extending module load path
- Exploring built-in modules
- Conclusion
Section 1- What is meant by modules?
In programming, a module is a piece of software that has a specific functionality. For example, when building a ping pong game, one module would be responsible for the game logic, and another module would be responsible for drawing the game on the screen. Each module is a different file, which can be edited separately.
At its core, a module in Python is a file containing Python definitions and statements. These files end with the .py extension and can define functions, classes, and variables. By encapsulating code within modules, developers can create a modular and organized codebase. Modules help in organizing code, promoting code reusability, and avoiding naming conflicts.
Modules in Python are simply Python files with the .py extension, which implement a set of functions. Modules are imported from other modules using the import command.
Consider a module to be the same as a code library. A file containing a set of functions you want to include in your application [2].
Section 2- Create a Module
Example 1
Modules in Python are simply Python files with a .py extension. The name of the module will be the name of the file. A Python module can have a set of functions, classes, or variables defined and implemented.
Let's start by creating a simple module. Consider a file named math_operations.py with the following content:
%%writefile math_operations.py
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
Here, we have defined three functions—add, subtract, and multiply. To use these functions in another Python script, we can import the math_operations module:
# main.py
import math_operations
result_add = math_operations.add(5, 3)
result_subtract = math_operations.subtract(8, 2)
result_multiply = math_operations.multiply(4, 6)
print("Addition:", result_add)
print("Subtraction:", result_subtract)
print("Multiplication:", result_multiply)
result_multiply)
In the main.py script, we import the math_operations module and use its functions as if they were defined locally. This promotes code reuse and maintains a clean code structure.
Example 2
In the example below, we will have two files, we will have:
mygame/ mygame/game.py mygame/draw.py
The Python script game.py will implement the game. It will use the function draw_game from the file draw.py, or in other words, the draw module, that implements the logic for drawing the game on the screen.
Modules are imported from other modules using the import command. In this example, the game.py script may look something like this:
# game.py # import the draw module import draw def play_game(): ... def main(): result = play_game() draw.draw_game(result) # this means that if this script is executed, then main() will be executed if __name__ == '__main__': main()
The draw module may look something like this:
# draw.py def draw_game(): ... def clear_screen(screen):
In this example, the game module imports the draw module, which enables it to use functions implemented in that module. The main function would use the local function play_game to run the game, and then draw the result of the game using a function implemented in the draw module called draw_game. To use the function draw_game from the draw module, we would need to specify in which module the function is implemented, using the dot operator. To reference the draw_game function from the game module, we would need to import the draw module and only then call a draw.draw_game().
When the import draw directive runs, the Python interpreter will look for a file in the directory which the script was executed from, by the name of the module with a .py prefix, so in our case, it will try to look for draw.py. If it finds one, it will import it. If not, he will continue to look for built-in modules.
You may have noticed that when importing a module, a .pyc file appears, which is a compiled Python file. Python compiles files into Python bytecode so that it won't have to parse the files each time modules are loaded. If a .pyc file exists, it gets loaded instead of the .py file, but this process is transparent to the user.
Example 3
Creating a module is as simple as writing code in a Python file and saving it with a ".py" extension. For example, if you have a file named "my_module.py," you can import it in another script using the import keyword:
# my_module.py
def greet(name):
return f"Hello, {name}!"
# main_script.py
import my_module
result = my_module.greet("Alice")
print(result)
This approach keeps code organized and allows developers to compartmentalize functionality into manageable units.
Section 3- Creating Python modules in Google Colab
Creating Python modules in Google Colab is similar to creating modules in any other Python environment. Here's a simple step-by-step guide with a sample example: Creating Python modules in Google Colab is similar to creating modules in any other Python environment. Here's a simple step-by-step guide with a sample example:
-
Create a new Colab notebook: Open Google Colab and create a new notebook.
-
Write your module code: In a new code cell, write the code for your Python module. Let's create a simple module named
my_modulethat contains a functiongreet: -
# Save this cell as "my_module.py"
def greet(name):
return f"Hello, {name}!"
-
After writing the code, run the cell to define the module.
-
Download the module: To use this module in other cells or notebooks, you can download it as a Python file. Go to File -> Download -> Python (.py). Save it with the name
my_module.py. -
Upload the module to Colab: If you have the module saved locally, you can upload it to Colab. Click on the folder icon on the left sidebar, then click on the upload button to upload your
my_module.pyfile. -
Import and use the module: Now, in a new cell or notebook, you can import and use your module:
from my_module import greet
result = greet("John")
print(result)
Make sure that the module file (my_module.py) is in the same directory or you provide the correct path when importing.
Section 4- Importing module objects to the current namespace
Example 1
We may also import the function draw_game directly into the main script's namespace, by using the from command.
# game.py
# import the draw module
from draw import draw_game
def main():
result = play_game()
draw_game(result)
You may have noticed that in this example, draw_game does not precede with the name of the module it is imported from, because we've specified the module name in the import command.
The advantages of using this notation is that it is easier to use the functions inside the current module because you don't need to specify which module the function comes from. However, any namespace cannot have two objects with the exact same name, so the import command may replace an existing object in the namespace.
Section 5- Importing all objects from a module
We may also use the import * command to import all objects from a specific module, like this:
# game.py
# import the draw module
from draw import *
def main():
result = play_game()
draw_game(result)
Section 6- Custom import name
We may also load modules under any name we want. This is useful when we want to import a module conditionally to use the same name in the rest of the code.
For example, if you have two draw modules with slighty different names - you may do the following:
# game.py
# import the draw module
if visual_mode:
# in visual mode, we draw using graphics
import draw_visual as draw
else:
# in textual mode, we print out text
import draw_textual as draw
def main():
result = play_game()
# this can either be visual or textual depending on visual_mode
draw.draw_game(result)
Section 7-Module initialization
The first time a module is loaded into a running Python script, it is initialized by executing the code in the module once. If another module in your code imports the same module again, it will not be loaded twice but once only - so local variables inside the module act as a "singleton" - they are initialized only once.
This is useful to know, because this means that you can rely on this behavior for initializing objects. For example:
# draw.py
def draw_game():
# when clearing the screen we can use the main screen object initialized in this module
clear_screen(main_screen)
...
def clear_screen(screen):
...
class Screen():
...
# initialize main_screen as a singleton
main_screen = Screen(
Section 7-Extending module load path
There are a couple of ways we could tell the Python interpreter where to look for modules, aside from the default, which is the local directory and the built-in modules. You could either use the environment variable PYTHONPATH to specify additional directories to look for modules in, like this:
PYTHONPATH=/foo python game.py
This will execute game.py, and will enable the script to load modules from the foo directory as well as the local directory.
Another method is the sys.path.append function. You may execute it before running an import command:
sys.path.append("/foo"
This will add the foo directory to the list of paths to look for modules in as well.
Section 8-Exploring built-in modules.
Check out the full list of built-in modules in the Python standard library here.
Two very important functions come in handy when exploring modules in Python - the dir and help functions.
If we want to import the module urllib, which enables us to create read data from URLs, we simply import the module:
# import the library
import urllib
# use it
urllib.urlopen(...)
help(urllib.urlopen)
Conclusion:
Modules are indispensable tools in Python development. They promote code organization, reusability, and collaboration, making it easier to manage and scale projects. Understanding these concepts allows developers to write more modular and maintainable code, a key aspect of Python’s success in the software development community. Whether you’re a beginner or an experienced Pythonista, mastering modules and packages is essential for writing efficient and scalable Python applications.