Python Modules and Packages: The Complete Guide with Examples What is a Python Module? The functions and variables you have defined are lost if you exit the Python interpreter and reenter it. It is therefore preferable to use a text editor to prepare the input for the interpreter and run it using that file as input if you wish to construct a slightly lengthier program. This is referred to as writing a script. For easier maintenance, you might want to divide your software into multiple files as it grows longer. Additionally, you might wish to use a useful function you’ve written in multiple apps without having to duplicate its definition in each one. Python has definitions in a file that can be used in scripts or interactive interpreter instances to support this. Such a file is called a module; definitions from a module can be imported into other modules or into the main module (the collection of variables that you have access to in a script executed at the top level and in calculator mode). A file with Python definitions and statements is called a module. The module name with the suffix “.py” is the file name. The value of the global variable name within a module can be the module’s name (as a string). For example, create a file named fibo.py in the current directory using your preferred text editor and fill it with the following information: Python’s modularity is what gives it its power. Python packages and modules with practical examples. This tutorial will show you how to efficiently structure and reuse your code, regardless of your level of experience. A module in Python is simply a file containing Python code — it can define functions, classes, and variables. You can also read for:- Lists, Tuples, Sets, and Dictionaries in Python 🔹 Why Use Modules? Code reusability Better organization Easier debugging Improved maintainability 🧾 Creating a Simple Module Let’s say we have a file named math_utils.py: # math_utils.py def add(x, y): return x + y def multiply(x, y): return x * y 🧪 Using the Module Create another file: # main.py import math_utils print(math_utils.add(2, 3)) # Output: 5 print(math_utils.multiply(2, 3)) # Output: 6 You’ve now separated logic and usage — this is the essence of modular programming. 🧳 Built-in Python Modules There are several built-in modules in Python. Let’s examine a few well-known ones: 🔧 1. math – Mathematical Functions import math print(math.sqrt(25)) # Output: 5.0 print(math.pi) # Output: 3.141592... 📅 2. datetime – Date and Time Handling from datetime import datetime now = datetime.now() print(now.strftime("%Y-%m-%d %H:%M:%S")) 📂 3. os – Operating System Interactions import os print(os.getcwd()) # Get current working directory os.mkdir("test_folder") # Create new directory 📜 4. sys – System-Specific Parameters import sys print(sys.version) # Show Python version print(sys.argv) # Command-line arguments 🔐 5. random – Random Number Generation import random print(random.randint(1, 10)) # Random number between 1 and 10 🌐 6. requests (3rd-party) – HTTP Requests import requests response = requests.get("https://api.github.com") print(response.status_code) 📦 What is a Python Package? A package is a directory that includes a unique init.py file and several modules. Python’s module namespace can be organized using packages and “dotted module names.” Take the module name A, for instance. B denotes a submodule in package A with the name B. The writers of multi-module packages like NumPy or Pillow can avoid worrying about each other’s module names by using dotted module names, much as the use of modules relieves the authors of various modules of the burden of naming each other’s global variables. Let’s say you wish to create a set of modules (a “package”) that will allow sound files and sound data to be handled consistently. Since there are numerous sound file formats (which are typically identified by their extensions, such as wav, aiff, and au), you might need to build and manage an expanding library of modules for file format conversion. Additionally, you will be constructing an endless stream of modules to carry out the various operations you may want to conduct on sound data, like mixing, adding echo, applying an equalizer function, and producing an artificial stereo effect. 📁 Structure Example: my_package/ │ ├── __init__.py ├── math_utils.py └── string_utils.py 🔍 Contents: math_utils.py def add(a, b): return a + b string_utils.py def shout(text): return text.upper() + "!" init.py from .math_utils import add from .string_utils import shout 🧪 Using the Package: # main.py from my_package import add, shout print(add(10, 5)) # Output: 15 print(shout("hello")) # Output: HELLO! 📚 When to Use Modules vs Packages? Use Case Module Package Few functions or classes ✅ Yes ❌ Not necessary Grouping multiple modules ❌ No ✅ Yes Single-purpose utility ✅ Yes ❌ Overkill Scalable projects ❌ No ✅ Recommended 🛠 Real-Life Example: Weather App Structure weather_app/ │ ├── __init__.py ├── fetch_data.py # API calls ├── parse_response.py # JSON parsing └── display.py # UI logic Expert Advice on Using Modules and Packages To manage imports, use all in init.py. Carefully arrange dependencies to prevent circular imports. For uniformity and clarity, use absolute imports. Observe naming rules by giving files lowercase letters and underscores.