Metadata-Version: 2.5
Name: zhijie-packaging-demo
Version: 0.1.0
Summary: A small package for practicing Python builds and publishing
Requires-Python: >=3.10
Description-Content-Type: text/markdown

# Package-Building Practice

This library contains several modules and two subpackages with simple text, number, and report functions. The goal is to practice building, inspecting, installing, and publishing a Python package yourself.

## 1. Understand the files

```text
package-demo/
├── pyproject.toml             Project metadata and build configuration
├── README.md                 This guide
├── src/
│   └── packaging_demo/
│       ├── __init__.py        Public imports
│       ├── helpers.py         add() and greet()
│       ├── reports.py         Combines text and number utilities
│       ├── text/             A subpackage
│       │   ├── __init__.py
│       │   ├── formatting.py
│       │   └── validation.py
│       └── numbers/          Another subpackage
│           ├── __init__.py
│           ├── arithmetic.py
│           └── statistics.py
└── tests/
    ├── test_helpers.py
    └── test_subpackages.py
```

Three names have different purposes:

- Project folder: `package-demo`.
- Distribution name: `zhijie-packaging-demo`, used with pip and on the index.
- Import name: `packaging_demo`, used in Python code.

The build frontend is `build`. It delegates artifact creation to the Hatchling backend declared in `pyproject.toml`. Build-time dependencies are separate from runtime dependencies; this package has no third-party runtime dependencies.

### Modules, subpackages, and imports

`reports.py` is a module. `text` and `numbers` are subpackages because each directory contains `__init__.py`. Their individual `.py` files are modules too. The whole tree is shipped as one distribution, not a separate pip package for every directory.

After installation, try:

```python
from packaging_demo import add, build_summary
from packaging_demo.text import title_case
from packaging_demo.numbers.statistics import average

print(add(2, 3))  # 5
print(title_case("hello world"))  # Hello World
print(average([10.0, 20.0, 30.0]))  # 20.0
print(build_summary("monthly sales", [10.0, 20.0, 30.0]))
# Monthly Sales: average = 20.00
```

These demonstrate imports from the top-level package, a subpackage, and a specific module. The `__init__.py` files re-export selected functions so callers can use shorter imports. Inside `reports.py`, relative imports such as `from .text import title_case` refer to sibling subpackages.

The wheel configuration remains `packages = ["src/packaging_demo"]`: the selected package tree includes these nested subpackages. You do not need to list every module separately. After building, inspect the wheel and run the tests against the installed artifact to verify that the nested files are present.

## 2. Prepare your environment

Open a terminal in this directory, where `pyproject.toml` is located. Run all subsequent commands from here unless instructed otherwise.

```bash
python -m venv .venv
source .venv/bin/activate
python -m pip install build twine
```

Before publishing, choose a distribution name available on your target repository. Availability of `zhijie-packaging-demo` has not been checked. If you change `[project].name`, use the new name in installation commands below. The import name can remain `packaging_demo`.

## 3. Build

```bash
python -m build
```

For a first build, the expected artifacts are:

```text
dist/
├── zhijie_packaging_demo-0.1.0.tar.gz
└── zhijie_packaging_demo-0.1.0-py3-none-any.whl
```

The wheel is a ZIP-format installable distribution. The source distribution contains the inputs needed to build the project. These functions remain Python source; building this example does not compile them into machine code.

Names above assume unchanged metadata. Always check the actual contents of `dist/`.

## 4. Inspect the artifacts

```bash
python -m twine check dist/*
python -m zipfile -l dist/zhijie_packaging_demo-0.1.0-py3-none-any.whl
python -m tarfile -l dist/zhijie_packaging_demo-0.1.0.tar.gz
```

The wheel should contain `packaging_demo` and distribution metadata. The sdist should contain source, tests, README, and build configuration. Review for unexpected or private files before uploading. Twine's check does not replace unit tests or a security review.

## 5. Install the wheel into a clean environment

```bash
python -m venv .venv-test
.venv-test/bin/python -m pip install dist/zhijie_packaging_demo-0.1.0-py3-none-any.whl
.venv-test/bin/python -m unittest discover -s tests -v
.venv-test/bin/python -c "from packaging_demo import add, greet; print(add(2, 3)); print(greet('Bradley'))"
```

Expected demonstration output:

```text
5
Hello, Bradley!
```

This tests the installed wheel, not an editable source install. Do not set `PYTHONPATH=src` for this check, since that could hide missing files in your wheel.

## 6. Practice publishing on TestPyPI

TestPyPI is a separate practice index and requires its own account and credentials. Register at [TestPyPI](https://test.pypi.org/account/register/), follow its account setup requirements, and create an API token. Do not put the token into code or this README.

Review `dist/`, then upload only the intended release:

```bash
python -m twine upload --repository testpypi dist/*
```

For token authentication, the username is `__token__` and the password is your TestPyPI token. Supply credentials securely when prompted; do not paste them into a command recorded in shell history.

Install your uploaded release into a separate environment:

```bash
python -m venv .venv-index
.venv-index/bin/python -m pip install --index-url https://test.pypi.org/simple/ --no-deps zhijie-packaging-demo==0.1.0
.venv-index/bin/python -m unittest discover -s tests -v
```

`--no-deps` is appropriate here because this example has no runtime dependencies. It is not a general instruction for packages that need dependencies. See the [official TestPyPI guide](https://packaging.python.org/en/latest/guides/using-testpypi/).

## 7. Publish to real PyPI only when ready

Real PyPI uses a separate account/token and has a separate name registry. Confirm that you intend to make the package public, have the rights to distribute its contents, and have selected an appropriate license before a real release. No license grant has been chosen for you in this exercise.

For an intentional public release, upload the reviewed artifacts:

```bash
python -m twine upload dist/*
```

Consumers can then install the published distribution using `python -m pip install zhijie-packaging-demo==0.1.0`. Adjust the name/version if changed.

Do not assume a previously uploaded artifact can be overwritten. For a new release, increment the version and build into a fresh output directory, such as `python -m build --outdir dist/0.1.1`. Check and upload only that release's files; do not accidentally upload stale artifacts.

For a private repository, use its documented Twine upload endpoint and pip index URL instead. These URLs need not be identical.

## Explain the process in your own words

> I configure the project in `pyproject.toml`, build a wheel and a source distribution, inspect the artifacts, and test the installed wheel in a clean environment. Then I upload the release to a package index and install it from that index.

The example source has been created for you. Building, installing, and publishing are left for you to perform.
