Unit 3 - 3.3 Packages in Python
1. What
is a Package in Python?
A package in Python is a collection of related modules stored in a directory. It helps in organizing large codebases by grouping related functionality together.
A package must contain a special file named __init__.py (though optional in Python 3.3+, it's recommended to explicitly include it).
2.
Structure of a Python Package
A package is
simply a directory containing multiple Python modules.
Example Package Structure:
mypackage/
│── __init__.py
│──
module1.py
│──
module2.py
│──
subpackage/
│ ├── __init__.py
│ ├── module3.py
- mypackage/ → Main package
directory
- __init__.py → Makes the
directory a package
- module1.py and module2.py →
Contain Python functions/classes
- subpackage/ → A package within a
package
3.
Creating a Python Package
Step 1:
Create a Package Directory
Let's create a package named mypackage.
mkdir
mypackage
Step 2:
Add an __init__.py File
Create a file inside mypackage/ named __init__.py.
#
__init__.py
print("mypackage
is imported")
Step 3:
Create Some Modules
Create two
Python files inside mypackage/:
module1.py
def greet(name):
return f"Hello, {name}!"
module2.py
def add(a, b):
return a + b
4.
Importing a Package in Python
After
creating the package, we can now use it in another Python script.
a) Importing the Whole Package
import
mypackage.module1
import mypackage.module2
print(mypackage.module1.greet("Alice")) # Output: Hello, Alice!
print(mypackage.module2.add(5,
3)) # Output: 8
b) Importing Specific Functions
from
mypackage.module1 import greet
from
mypackage.module2 import add
print(greet("Bob")) # Output: Hello, Bob!
print(add(10,
2)) # Output: 12
c)
Importing All Modules (Using __init__.py)
Modify __init__.py to automatically import modules:
from
.module1 import greet
from
.module2 import add
Now, you can directly import the package:
import mypackage
print(mypackage.greet("Charlie")) # Output: Hello, Charlie!
print(mypackage.add(7,
3)) # Output: 10
5.
Subpackages in Python
A subpackage
is a package inside another package.
Creating a Subpackage
mypackage/
│── __init__.py
│──
module1.py
│──
subpackage/
│ ├── __init__.py
│ ├── module3.py
subpackage/module3.py
def multiply(a,
b):
return a * b
Importing from a Subpackage
from
mypackage.subpackage.module3 import multiply
print(multiply(4,
5)) # Output: 20
6.
Installing and Using External Packages
Python
allows installing third-party packages using pip.
Installing a Package
pip install
requests
Using an Installed Package
import requests
response =
requests.get("https://www.google.com")
print(response.status_code) # Output: 200
7.
Advantages of Using Packages
✅ Modularity – Organizes large
codebases.
✅ Reusability – Functions can be reused across multiple
projects.
✅ Avoids Name Conflicts – Helps prevent
variable/function name clashes.
✅ Scalability – Makes it easy to expand and maintain
projects.
8.
Summary
- A package is a directory containing
multiple modules and an __init__.py file.
- Subpackages allow organizing related
functionality even further.
- Importing can be done using import package.module, from
package import module, or from package import *.
- Third-party packages can be installed using pip.
No comments:
Post a Comment