A package is a directory of related modules. Where a module is a single .py file, a package groups several modules (and optionally other packages) under one name, so a larger project stays organized and its parts can be reused or shared.
Terms to Know
- Module: one Python file with reusable code.
- Package: a directory that holds modules, normally with an
__init__.pyfile. - Sub-package: a package nested inside another package.
Historically, __init__.py is what marks a folder as a package. Since Python 3.3, a folder without it can still be imported as a namespace package, but for ordinary projects you should include the file. It also runs when the package is first imported, which makes it the place to expose a clean public interface.
Building a Package Step by Step
- Create a folder that will be the package root.
- Add your modules (
.pyfiles) inside it. - Add an
__init__.pyfile (it can be empty). - Optionally add sub-folders, each with its own
__init__.py. - Import from the package using dot notation, for example
from store.pricing import add_tax.
Example: A store Package
We'll build a small package for an online shop. It has two sub-packages: pricing (discounts and tax) and stock (inventory checks and updates).
store/
__init__.py
pricing/
__init__.py
discount.py
tax.py
stock/
__init__.py
check.py
update.pystore/pricing/discount.py
def apply_discount(price, percent):
return price - price * percent / 100store/pricing/tax.py
def add_tax(price, rate):
return price * (1 + rate / 100)store/pricing/__init__.py: exposes both functions at the sub-package level.
from .discount import apply_discount
from .tax import add_taxstore/stock/check.py
def in_stock(inventory, item):
return inventory.get(item, 0) > 0store/stock/update.py
def restock(inventory, item, quantity):
inventory[item] = inventory.get(item, 0) + quantity
return inventorystore/stock/__init__.py
from .check import in_stock
from .update import restockstore/__init__.py: re-exports the most-used functions so users can import them straight from store.
from .pricing import apply_discount, add_tax
from .stock import in_stock, restockThe leading dot in from .pricing import ... is a relative import, meaning "from this package". It keeps working even if you rename the top-level folder.
Using the Package
from store import apply_discount, add_tax, in_stock, restock
price = apply_discount(200, 15)
print(price)
print(round(add_tax(price, 18), 2))
inventory = {"pen": 0, "book": 5}
print(in_stock(inventory, "pen"))
restock(inventory, "pen", 10)
print(in_stock(inventory, "pen"))
print(inventory)Output:
170.0
200.6
False
True
{'pen': 10, 'book': 5}Other Ways to Import From It
import store
print(store.add_tax(100, 10))
from store.pricing.tax import add_tax # go straight to the module
import store.stock.check as check # alias for a deep pathControlling What import * Exposes
Define __all__ in a package's __init__.py to list the names that from package import * should bring in:
# store/__init__.py
__all__ = ["apply_discount", "add_tax", "in_stock", "restock"]From Local Package to Shareable Library
A package sitting in your project folder only works when Python can find it on sys.path. To share it or install it in other environments, you add packaging metadata (usually a pyproject.toml), build it, and publish it to PyPI, the index that pip install pulls from. Once a package is installed this way, it is imported exactly like your local one.
Popular Third-Party Packages by Purpose
Install any of these with pip install <name>.
Web frameworks (routing, request handling, database integration):
Flask, Django, FastAPI, Pyramid, Tornado, Falcon, CherryPy, Bottle
Data analysis and numerical computing:
NumPy, Pandas, SciPy, StatsModels, Dask
Classical machine learning:
Scikit-learn, XGBoost
Deep learning:
TensorFlow, PyTorch, Keras, Fastai
Natural language processing:
NLTK, spaCy, FastText, Transformers (Hugging Face), TextBlob, AllenNLP
Data visualization:
Matplotlib, Seaborn, Plotly, Bokeh, Altair, Pygal, Plotnine, Yellowbrick (for ML model diagnostics)
Computer vision and image processing:
OpenCV, scikit-image, Pillow, ImageIO, Dlib, Mahotas
Dashboards:
Dash, Streamlit
Desktop GUI:
Tkinter (included with Python), PyQt5, PySide, Kivy, PySimpleGUI, NiceGUI, PyGObject (the maintained successor to PyGTK)
Web scraping and automation:
Requests, BeautifulSoup, Selenium, urllib3, Scrapy, lxml, PyAutoGUI, schedule, Watchdog
Game development:
Pygame, Pyglet, Arcade
Quick Recap
A package is a folder of modules made importable by __init__.py. Sub-packages let you nest that structure, relative imports keep internal references stable, and re-exporting names in __init__.py gives users a simple import path. Packaged and published to PyPI, the same structure becomes a library anyone can install.