|
| 1 | +In Python, importing modules and creating packages helps in organizing and reusing code effectively. Let’s break down each concept with examples. |
| 2 | + |
| 3 | +### 1. **Importing Modules** |
| 4 | + |
| 5 | +A **module** in Python is simply a file containing Python definitions and statements. It can include functions, variables, and classes. You can import these modules into other scripts to use their functions without rewriting code. |
| 6 | + |
| 7 | +#### Example of Creating and Importing a Module |
| 8 | + |
| 9 | +1. **Create a Module** |
| 10 | + Suppose we create a file called `math_functions.py` with the following content: |
| 11 | + |
| 12 | + ```python |
| 13 | + # math_functions.py |
| 14 | + |
| 15 | + def add(a, b): |
| 16 | + return a + b |
| 17 | + |
| 18 | + def subtract(a, b): |
| 19 | + return a - b |
| 20 | + ``` |
| 21 | + |
| 22 | +2. **Import and Use the Module in Another File** |
| 23 | + Now, we can create a new file, say `main.py`, and import `math_functions` to use its functions. |
| 24 | + |
| 25 | + ```python |
| 26 | + # main.py |
| 27 | + import math_functions |
| 28 | + |
| 29 | + result_add = math_functions.add(5, 3) |
| 30 | + result_subtract = math_functions.subtract(5, 3) |
| 31 | + print("Addition:", result_add) |
| 32 | + print("Subtraction:", result_subtract) |
| 33 | + ``` |
| 34 | + |
| 35 | + **Output:** |
| 36 | + ``` |
| 37 | + Addition: 8 |
| 38 | + Subtraction: 2 |
| 39 | + ``` |
| 40 | + |
| 41 | +#### Importing Specific Functions |
| 42 | +If you only need certain functions, you can import them directly. |
| 43 | + |
| 44 | +```python |
| 45 | +# main.py |
| 46 | +from math_functions import add |
| 47 | + |
| 48 | +result = add(5, 3) |
| 49 | +print("Addition:", result) |
| 50 | +``` |
| 51 | + |
| 52 | +### 2. **Creating and Using Packages** |
| 53 | + |
| 54 | +A **package** is a way of organizing multiple modules. It’s essentially a directory with an `__init__.py` file and other modules. This allows you to organize related modules into a single namespace. |
| 55 | + |
| 56 | +#### Example of Creating a Package |
| 57 | + |
| 58 | +1. **Create a Package Directory Structure** |
| 59 | + Suppose we want a package called `calculator` with modules for basic and advanced operations. Create the following directory structure: |
| 60 | + |
| 61 | + ``` |
| 62 | + calculator/ |
| 63 | + ├── __init__.py |
| 64 | + ├── basic.py |
| 65 | + └── advanced.py |
| 66 | + ``` |
| 67 | + |
| 68 | +2. **Define Modules in the Package** |
| 69 | + |
| 70 | + - `basic.py`: |
| 71 | + |
| 72 | + ```python |
| 73 | + # basic.py |
| 74 | + |
| 75 | + def add(a, b): |
| 76 | + return a + b |
| 77 | + |
| 78 | + def subtract(a, b): |
| 79 | + return a - b |
| 80 | + ``` |
| 81 | + |
| 82 | + - `advanced.py`: |
| 83 | + |
| 84 | + ```python |
| 85 | + # advanced.py |
| 86 | + |
| 87 | + def multiply(a, b): |
| 88 | + return a * b |
| 89 | + |
| 90 | + def divide(a, b): |
| 91 | + if b == 0: |
| 92 | + return "Cannot divide by zero" |
| 93 | + return a / b |
| 94 | + ``` |
| 95 | + |
| 96 | +3. **Importing and Using the Package** |
| 97 | + You can now use `calculator` as a package by importing it in a script. |
| 98 | + |
| 99 | + ```python |
| 100 | + # main.py |
| 101 | + from calculator import basic, advanced |
| 102 | + |
| 103 | + print("Addition:", basic.add(5, 3)) |
| 104 | + print("Subtraction:", basic.subtract(5, 3)) |
| 105 | + print("Multiplication:", advanced.multiply(5, 3)) |
| 106 | + print("Division:", advanced.divide(5, 3)) |
| 107 | + ``` |
| 108 | + |
| 109 | + **Output:** |
| 110 | + ``` |
| 111 | + Addition: 8 |
| 112 | + Subtraction: 2 |
| 113 | + Multiplication: 15 |
| 114 | + Division: 1.6666666666666667 |
| 115 | + ``` |
| 116 | + |
| 117 | +#### Using `__init__.py` to Control Imports |
| 118 | +The `__init__.py` file can be used to define what is available when you import the package. For instance, if you want to make `basic` and `advanced` accessible directly, you could add the following to `__init__.py`: |
| 119 | + |
| 120 | +```python |
| 121 | +# __init__.py |
| 122 | +from .basic import add, subtract |
| 123 | +from .advanced import multiply, divide |
| 124 | +``` |
| 125 | + |
| 126 | +Now you can import directly from `calculator`: |
| 127 | + |
| 128 | +```python |
| 129 | +# main.py |
| 130 | +from calculator import add, multiply |
| 131 | + |
| 132 | +print("Addition:", add(5, 3)) |
| 133 | +print("Multiplication:", multiply(5, 3)) |
| 134 | +``` |
| 135 | + |
| 136 | +### Summary |
| 137 | +- **Modules** organize code into files you can import. |
| 138 | +- **Packages** group related modules, helping manage larger projects. |
0 commit comments