> For the complete documentation index, see [llms.txt](https://llm-aws.course.gspivey.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://llm-aws.course.gspivey.com/foundations/introduction-to-python/python-package.md).

# Python Package

In Python, a package is a way of organizing related modules into a directory hierarchy. Essentially, it's a directory that contains multiple module files and a special `__init__.py` file to let Python know that the directory is a package.

Here's a step-by-step guide on how to organize your modules into a package:

1. **Create a directory** - This directory will be your package. Let's call it `mypackage`.

```
mypackage/
```

2. **Add a `__init__.py` file** - This file is required for Python to recognize the directory as a package. It can be empty, but it can also execute initialization code for the package.

```
mypackage/
    __init__.py
```

3. **Add module files** - Now you can add your Python module files to the package.

```
mypackage/
    __init__.py
    module1.py
    module2.py
```

In this example, `module1.py` and `module2.py` are Python files with their own functions, classes, and variables.

4. **Use the package** - You can now import the modules in the package from another Python script using the package name and the module name.

```python
# main.py

from mypackage import module1, module2

module1.my_function()
module2.my_function()
```

In this example, `my_function` is a function defined in `module1.py` and `module2.py`.

5. **Subpackages** - Packages can also contain subpackages. A subpackage is just a package that is a subdirectory of another package.

```
mypackage/
    __init__.py
    module1.py
    module2.py
    subpackage/
        __init__.py
        module3.py
```

You can import modules from subpackages like this:

```python
from mypackage.subpackage import module3

module3.my_function()
```

Organizing your code into packages and modules keeps your codebase clean, manageable, and easier to reuse across different projects.
