Skip to content

Latest commit

 

History

History
107 lines (71 loc) · 2.6 KB

File metadata and controls

107 lines (71 loc) · 2.6 KB

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.

  • Creating a Module: To create a module, simply save Python code in a .py file.

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 import statement.
# 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.

  • Creating a Package: To create a package, create a directory and place an __init__.py file 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: 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

  • 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