Python Packages
📦 Python Packages
A package in Python is a collection of modules stored inside a folder.
Packages help in:
Organizing large programs
Reusing code
Structuring project files properly
🧩 Difference Between Module & Package
| Feature | Module | Package |
|---|---|---|
| Definition | A single .py file | A folder containing multiple modules |
| Example | math.py | requests, numpy |
| Contains | Functions, variables, classes | Modules, sub-packages |
📁 Package Structure Example
Create a folder named:
greetings.py
math_tools.py
__init__.py
Using the Package
Create another Python file:
Importing Specific Modules from a Package
🏷 What is __init__.py?
A special file that tells Python this folder is a package
Can be empty or contain initialization code
Used to control what gets imported when using:
🔥 Built-in Python Packages
Python already includes many packages:
| Package | Use |
|---|---|
json | JSON parsing |
urllib | URL handling |
email | Sending/receiving emails |
tkinter | GUI apps |
collections | Advanced data structures |
Example:
⬇ Installing External Packages Using pip
To install:
To use:
Uninstall a Package
Check Installed Packages
Create a Custom Package and Install It
Create folder structure:
tools.py:
Install locally (inside folder):
Now use anywhere:
📌 Summary
| Feature | Example |
|---|---|
| Create package | Folder with __init__.py |
| Import package | import my_package |
| Import module | from my_package import module |
| Install external package | pip install modulename |
🧠 Practice Tasks
Create a package named school with modules:
students.py(store student list)grades.py(function to calculate percentage)
Install and use the pyjokes or emoji package.
Create a package and import it using alias.
