# Turn an existing Python project into an installable package

Your script can pass its tests in the project folder and still fail after someone installs it. Python can find files in that folder even if they're missing from the package you build.

In this tutorial, you'll use uv to turn a working script into an installable command. You'll move the code into `src/`, separate the libraries the command needs from the tools you use to develop it, and try the finished package outside the project folder.

If you want to start with an empty directory, use [Set up a complete Python project](https://pydevtools.com/handbook/tutorial/set-up-a-complete-python-project.md). If the project already uses `setup.py`, follow [Migrate from setup.py to pyproject.toml](https://pydevtools.com/handbook/how-to/migrate-setup-py.md) instead of replacing its build configuration. If it already has a build configuration in `pyproject.toml`, keep it and adapt the steps you need.

## Check the repository before changing it

Install [uv](https://pydevtools.com/handbook/reference/uv.md) and commit or set aside unrelated changes. Run your script and tests before moving any files so you know what should still work afterward.

The sample repository is named `byte-format` and starts with this structure:

{{< /filetree/container >}}

`requirements.txt` contains `humanize`, while `requirements-dev.txt` contains [pytest](https://pydevtools.com/handbook/reference/pytest.md). The application accepts a byte count and prints a readable value:

```python {filename="converter.py"}
from __future__ import annotations

import argparse

import humanize


def format_bytes(value: int) -> str:
    return humanize.naturalsize(value, binary=True, format="%.1f")


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument("value", type=int)
    args = parser.parse_args()
    print(format_bytes(args.value))


if __name__ == "__main__":
    main()
```

The test imports directly from `converter.py`:

```python {filename="test_converter.py"}
from converter import format_bytes


def test_format_bytes() -> None:
    assert format_bytes(1_048_576) == "1.0 MiB"
```

Run the application and test in temporary environments built from the existing requirements files:

```console
$ uv run --with-requirements requirements.txt python converter.py 1048576
1.0 MiB
$ uv run --with-requirements requirements.txt --with-requirements requirements-dev.txt pytest -q
.                                                                        [100%]
1 passed in 0.01s
```

`uv run` executes the command inside the project's virtual environment, ensuring all dependencies are installed first. Before this repository becomes a project, `--with-requirements` instead creates a temporary environment from the old files.

Save the output from your own project so you can compare it after the move.

## Set up the package with uv

Run `uv init` in the repository root. Replace `byte-format` with the name people will install, and `3.12` with the oldest Python version your project supports:

```console
$ uv init --lib --name byte-format --author-from none --python 3.12 .
Initialized project `byte-format` at `/path/to/byte-format`
```

The `--lib` template adds {{< term "pyproject-toml" "pyproject.toml" >}}, `.python-version`, a `src/byte_format/` folder for your code, and a setting that tells uv to use `uv_build` as the {{< term "build-backend" "build backend" >}}. People install `byte-format` with a hyphen and import `byte_format` with an underscore.

`--author-from none` prevents uv from copying local Git identity into package metadata. uv uses `3.12` both for local development (`.python-version`) and as the minimum version users can install on (`requires-python`).

`uv_build` works for this Python-only project. If yours includes C, C++, or Rust extensions, see [how to package projects with native extensions](https://pydevtools.com/handbook/how-to/how-to-package-python-projects-with-native-extensions.md).

## Move the code into `src/`

Move the tracked module into the generated package directory:

```bash
git mv converter.py src/byte_format/converter.py
```

`git mv` prints nothing when it succeeds.

Replace the generated `src/byte_format/__init__.py` so users can import `format_bytes` directly from `byte_format`:

```python {filename="src/byte_format/__init__.py"}
from byte_format.converter import format_bytes

__all__ = ["format_bytes"]
```

Update the test to import the installed package instead of importing from the old `converter.py` location:

```python {filename="test_converter.py"}
from byte_format import format_bytes


def test_format_bytes() -> None:
    assert format_bytes(1_048_576) == "1.0 MiB"
```

If your project has several Python modules, move the modules users need into `src/<package_name>/` and update their imports to use that package name. Leave tests, documentation, CI configuration, and maintenance scripts at the repository root.

## Add the libraries and test tools

Import packages needed when users run the project:

```bash
uv add -r requirements.txt
```

Expect uv to add `humanize` under `[project.dependencies]`, create a {{< term "lockfile" >}} named `uv.lock`, and install the project in the {{< term "virtual-environment" "virtual environment" >}} at `.venv`.

Import test and development tools separately:

```bash
uv add --dev -r requirements-dev.txt
```

The `--dev` flag marks pytest as a {{< term "dev-dependency" "development dependency" >}}, keeping it out of production installs. Expect a `[dependency-groups]` table in `pyproject.toml` and pytest in the updated lockfile.

If either requirements file points to another file with `-r` or `-c`, review those files before importing. Check that libraries users need don’t end up in the development group and that any version limits carry over.

## Give users a command to run

Edit the generated description, then add a `[project.scripts]` table that maps the `byte-format` command to `main()`:

```toml {filename="pyproject.toml"}
[project]
name = "byte-format"
version = "0.1.0"
description = "Format byte counts for humans"
readme = "README.md"
requires-python = ">=3.12"
dependencies = [
    "humanize>=4.17.0",
]

[project.scripts]
byte-format = "byte_format.converter:main"

[build-system]
requires = ["uv_build>=0.13.9,<0.14.0"]
build-backend = "uv_build"

[dependency-groups]
dev = [
    "pytest>=9.2.0",
]
```

Use the version ranges that `uv add` and `uv init` put in your file. They may differ from this example.

Run the installed command and the updated test:

```console
$ uv run byte-format 1048576
1.0 MiB
$ uv run pytest -q
.                                                                        [100%]
1 passed in 0.01s
```

The command should print the same result as before. The test now imports the package uv installed rather than a loose file in the project folder.

Remove the old dependency files after both commands pass:

```console
$ git rm requirements.txt requirements-dev.txt
rm 'requirements-dev.txt'
rm 'requirements.txt'
```

If a deployment system still requires `requirements.txt`, make your dependency changes in `pyproject.toml`, then [export requirements from uv](https://pydevtools.com/handbook/how-to/how-to-export-requirements-txt-from-a-uv-project.md).

## Check that the lockfile is up to date

Sync from the lockfile without allowing uv to update it:

```bash
uv sync --locked
```

If uv says the lockfile is out of date, run `uv lock`, review what changed, then try `uv sync --locked` again.

Your project should now look like this:

{{< /filetree/folder >}}
  {{< /filetree/folder >}}
{{< /filetree/container >}}

Commit `pyproject.toml`, `uv.lock`, `.python-version`, the moved source, and the updated tests together. Do not commit `.venv/` or `dist/`.

## Build and check the package

Build a [source distribution](https://pydevtools.com/handbook/reference/sdist.md) (`.tar.gz`) and a {{< term "wheel" >}} (`.whl`). The wheel is the file you’ll install and test:

```console
$ uv build
Building source distribution...
Building wheel from source distribution...
Successfully built dist/byte_format-0.1.0.tar.gz
Successfully built dist/byte_format-0.1.0-py3-none-any.whl
```

List the wheel contents before publishing it:

```console
$ uv run python -m zipfile -l dist/byte_format-0.1.0-py3-none-any.whl
File Name                                             Modified             Size
byte_format/__init__.py                        1980-01-01 00:00:00           75
byte_format/converter.py                       1980-01-01 00:00:00          386
byte_format/py.typed                           1980-01-01 00:00:00            0
byte_format-0.1.0.dist-info/entry_points.txt   1980-01-01 00:00:00           60
...
```

Look for your Python files and `entry_points.txt`, which tells Python packaging tools to create the `byte-format` command. The build can succeed even if it leaves out a file you need.

Install the wheel in a temporary environment and run the command:

```console
$ uv run --no-project --with ./dist/byte_format-0.1.0-py3-none-any.whl byte-format 1048576
Installed 2 packages in 6ms
1.0 MiB
```

`--no-project` prevents uv from reading the local `pyproject.toml`, while `--with` installs the wheel and its dependency into a temporary environment. If you see `1.0 MiB` again, the command works from the wheel without relying on files in your project folder.

## Review the conversion before committing

- The old application and tests passed before files moved.
- `pyproject.toml` lists what users need separately from what you need for development.
- Importable code lives under `src/<package_name>/`, while tests stay outside the package.
- Tests import the package name instead of a root-level module or `src`.
- `uv.lock` is current, and `uv sync --locked` succeeds.
- The wheel contains your code and the command definition.
- The command works when installed from the wheel in a temporary environment.

The repository is now an installable package. Continue with [the complete local quality stack](https://pydevtools.com/handbook/tutorial/set-up-a-complete-python-project.md) to add Ruff, Pyrefly, and commit hooks, or [build and publish the package](https://pydevtools.com/handbook/tutorial/build-and-publish-a-python-package.md) when you’re ready to try publishing on TestPyPI.
