Python Modules, Packages and pip Explained

Part 9 of the Python for Everyone track. Last updated: September 2026.

Real Python programs are never one file. Modules split code across files, packages group modules into directories, and pip brings in the world's libraries. This post covers all three plus the standard library gems you should know.

Importing: the four forms

import math                 # use as math.sqrt(16)
import math as m            # alias: m.sqrt(16)
from math import sqrt       # import one name: sqrt(16)
from math import sqrt as s  # alias a single name

Prefer import module over from module import * — star imports pollute your namespace and hide where names came from.

Writing your own module

A module is just a .py file. Create helpers.py:

# helpers.py
def shout(text):
    return text.upper() + "!"

PI_APPROX = 3.14

Use it from another file in the same folder:

# main.py
import helpers
print(helpers.shout("hello"))  # HELLO!

The __name__ == "__main__" idiom

# helpers.py
def shout(text):
    return text.upper() + "!"

if __name__ == "__main__":
    # runs ONLY when this file is executed directly, not when imported
    print(shout("self test"))

This is how you make a file both importable and runnable — you'll see it in nearly every real-world script.

Packages: modules in folders

myapp/
    __init__.py      # marks the folder as a package (can be empty)
    helpers.py
    database.py
from myapp import helpers
from myapp.database import connect

pip beyond the basics

pip install "requests>=2.0"   # version constraints
pip install -U requests        # upgrade to latest
pip uninstall requests         # remove
pip freeze                     # exact snapshot for requirements.txt

Always install into an active virtual environment (see Part 1) — and pin versions in requirements.txt so teammates get identical setups.

Standard library tour: batteries included

import os, sys, json, datetime, random, math
from pathlib import Path

print(Path.home())                    # your home directory, cross-platform
print(datetime.date.today())          # 2026-09-28
print(random.choice(["a", "b"]))      # pick randomly
data = json.loads('{"x": 1}')         # parse JSON → dict
print(os.environ.get("HOME"))         # read environment variables
print(sys.version)                    # interpreter version

Before reaching for pip, check the stdlib — os, sys, json, datetime, pathlib, collections, itertools cover a huge amount.

Key takeaways

  • A module is a .py file; a package is a folder of modules with __init__.py.
  • if __name__ == "__main__": makes files both importable and runnable.
  • Avoid from x import *; pin versions in requirements.txt.
  • Learn the stdlib first — it's bigger than you think.

Next in this series: Python File Handling: Read, Write, JSON and CSV.

Comments

Popular posts from this blog

Java Banking Finance Services and Insurance (BFSI) domain interview questions

JSP Servlet Interview Questions For Freshers Series 1

Java program to check even or odd number