Code-Memo

Modules and Packages

Modules

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.

Example module math_operations.py:

# math_operations.py

def add(a, b):
    return a + b

def subtract(a, b):
    return a - b
# main.py

import math_operations

result = math_operations.add(5, 3)
print(result)  # Output: 8

Packages

Packages are namespaces that contain multiple modules. They allow you to structure your Python project by organizing modules hierarchically.

Example package structure:

my_package/
│
├── __init__.py
├── module1.py
└── module2.py
# 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: 2

Packaging Python Code for Distribution

Python 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.

Steps to Package Python Code

  1. Create setup.py: This file defines package metadata and dependencies using setuptools.

Example setup.py:

from setuptools import setup, find_packages

setup(
    name='my_package',
    version='1.0',
    packages=find_packages(),
    install_requires=[
        'dependency1',
        'dependency2',
    ],
)
  1. Include __init__.py: Ensure all directories that should be treated as packages have an __init__.py file.

  2. Build the Package: Use setuptools to build a distribution package.

python setup.py sdist
  1. Distribution: Distribute the package via PyPI (Python Package Index) or other repositories for installation using pip.
pip install my_package

Benefits of Packaging