Modules in Python are files containing Python code. They allow you to organize code into files for better maintainability, reusability, and to avoid namespace collisions.
- Creating a Module: To create a module, simply save Python code in a
.pyfile.
Example module math_operations.py:
# math_operations.py
def add(a, b):
return a + b
def subtract(a, b):
return a - b- Using a Module: Modules are imported using the
importstatement.
# main.py
import math_operations
result = math_operations.add(5, 3)
print(result) # Output: 8Packages are namespaces that contain multiple modules. They allow you to structure your Python project by organizing modules hierarchically.
- Creating a Package: To create a package, create a directory and place an
__init__.pyfile inside it. This file can be empty or contain initialization code.
Example package structure:
my_package/
│
├── __init__.py
├── module1.py
└── module2.py
- Using a Package: Packages are imported using dot notation.
# main.py
import my_package.module1
from my_package import module2
result1 = my_package.module1.add(5, 3)
result2 = module2.subtract(5, 3)
print(result1) # Output: 8
print(result2) # Output: 2Python code can be packaged and distributed for installation using tools like setuptools and pip. Packaging involves defining metadata, dependencies, and installation instructions for your project.
- Create
setup.py: This file defines package metadata and dependencies usingsetuptools.
Example setup.py:
from setuptools import setup, find_packages
setup(
name='my_package',
version='1.0',
packages=find_packages(),
install_requires=[
'dependency1',
'dependency2',
],
)-
Include
__init__.py: Ensure all directories that should be treated as packages have an__init__.pyfile. -
Build the Package: Use
setuptoolsto build a distribution package.
python setup.py sdist- Distribution: Distribute the package via PyPI (Python Package Index) or other repositories for installation using
pip.
pip install my_package- Reusability: Easily reuse and share code across different projects.
- Dependency Management: Specify and manage project dependencies.
- Distribution: Facilitates easy installation and distribution of Python projects.
Go Back