In Python, a module is simply a file containing Python code—such as functions, classes, and variables—that you can reuse in other programs. By separating related functionalities into distinct modules, you can organize your code more effectively, reduce duplication, and make maintenance easier. Modules also facilitate collaboration by allowing different team members to work on separate files without interfering with each other’s code.
# Creating a module named 'my_module.py'
def greet(name):
print(f"Hello, {name}!")
# Using the module in another Python script
import my_module
my_module.greet('Alice')
The import statement lets you bring in the module’s functionality under a
specified namespace. You can also use from my_module import greet to import
specific functions or attributes into your current namespace.
if __name__ == "__main__": Guard
When you import a module, Python executes all the code inside it immediately. Sometimes, you want code to
run only when the file is executed directly (e.g., for testing), but not when it is
imported by another script. This is achieved using the __name__ check.
# Inside my_module.py
def greet(name):
print(f"Hello, {name}!")
if __name__ == "__main__":
# This block runs ONLY if you run 'python my_module.py' directly
# It is skipped if you 'import my_module' elsewhere
greet("Developer")
Python includes a rich standard library of built-in modules. These modules cover common tasks—like math operations, file I/O, and system-level operations—so you can address many needs without installing extra packages.
Aliasing: In Data Science and AI, it is standard convention to rename modules upon import
using the as keyword (e.g., import pandas as pd, import numpy as np).
import math as m
# Using the alias 'm' instead of 'math'
result = m.sqrt(25)
print(f"Square root: {result}")
A package is a collection of related modules organized into a directory structure.
Packages let you group modules by functionality. Each package typically contains an
__init__.py file (even if empty) to tell Python that the directory is a
package.
# Creating a package named 'ai_tools'
# Directory structure:
# ai_tools/
# __init__.py
# text_utils.py
# Inside 'text_utils.py':
def clean_text(text):
return text.strip().lower()
# Using the package in another script
from ai_tools import text_utils
raw = " Hello AI! "
print(f"Cleaned: '{text_utils.clean_text(raw)}'")
Before installing third-party packages, you should always create a Virtual Environment. This creates an isolated sandbox for your project, ensuring that the libraries you install for one project (e.g., using TensorFlow 2.0) don't conflict with another project (e.g., using TensorFlow 1.0).
# 1. Create a virtual environment named '.venv'
python -m venv .venv
# 2. Activate it
# Windows:
.venv\Scripts\activate
# Mac/Linux:
source .venv/bin/activate
# 3. Now, when you run 'pip install', packages are stored safely in .venv
Python’s ecosystem extends far beyond the standard library. The Python Package Index (PyPI)
hosts hundreds of thousands of packages. You can install these packages using pip.
# Installing a package
pip install requests
# Saving your dependencies to a file (Standard Practice)
pip freeze > requirements.txt
Once installed, you can import and use these packages just like built-in modules:
import requests.
import torch (PyTorch - Neural Networks)import transformers (Hugging Face - LLMs)import pandas (Data Manipulation)
You can distribute your own packages to share code with colleagues or the Python community. Modern Python
packaging uses a pyproject.toml file to define configuration and dependencies
(replacing the older setup.py standard).
# Basic structure for a modern Python project
# my_project/
# pyproject.toml <-- Configuration
# src/
# my_package/
# __init__.py
# main.py
# README.md
AI can guide your search for ideal packages. Instead of asking “Which packages do I need?”, specify your goal: “I need to parse and format dates in Python—what libraries work for that?”
Example Prompt:Resulting AI-generated code:
# Prerequisite: pip install python-dateutil
from dateutil import parser
# Parsing a messy date string
date_str = "May 27th, 2024 at 10:30am"
parsed_date = parser.parse(date_str)
print(f"Parsed object: {parsed_date}")
# Formatting the date to ISO 8601
formatted_date = parsed_date.strftime("%Y-%m-%d %H:%M:%S")
print(f"Clean format: {formatted_date}")