Publish Your Own Python Package in Less Than 10 Minutes

NeuralNineAbout 3 min readJun 17, 2025Watch original
THE SUMMARYAI-generated

Key Concepts

  • UV: A modern Python package installer and resolver, positioned as a faster alternative to pip.
  • PyPI (pypi.org): The Python Package Index, a repository for Python packages.
  • Package Project: A directory containing the source code, __init__.py, and pyproject.toml file, which defines the package's metadata and dependencies.
  • pyproject.toml: A configuration file that specifies build requirements and package metadata.
  • API Token: A security key used to authenticate package uploads to PyPI.
  • Environment Variables: Dynamic-named values that can affect the way running processes will behave on a computer.
  • uv build: A command to build a Python package into a distributable format.
  • uv publish: A command to upload a built Python package to PyPI.

Setting Up the Environment

  1. Install UV: The video demonstrates installing UV using pip install uv or a Linux package manager (e.g., sudo pacman -S python-uv on Arch Linux).
  2. Create a PyPI Account: Register an account on pypi.org and verify the email address.
  3. Generate an API Token: Log in to PyPI, navigate to account settings, and create a new API token. Copy and securely store this token.

Creating a Package Project

  1. Initialize the Project: Use the command uv init -p <package_name> to create a new package project directory. For example: uv init -p my_tutorial_calculator. The package name must be unique on PyPI.
  2. Write the Source Code:
    • Create a source directory (if it doesn't exist).
    • Create Python files containing the package's functionality. In the example, operations.py contains simple arithmetic functions (add, sub, mole, diff).
    • Populate the __init__.py file to make the functions accessible when the package is imported. The example uses from .operations import add, sub, mole, diff and defines __all__ = ['add', 'sub', 'mole', 'diff'].
    • Example operations.py content:
    def add(x, y):
        return x + y

    def sub(x, y):
        return x - y

    def mole(x, y):
        return x * y

    def diff(x, y):
        return x / y

Configuring pyproject.toml

  1. Modify Metadata: Edit the pyproject.toml file to include relevant package metadata:
    • requires-python: Specify the required Python version (e.g., ">=3.10").
    • description: Add a description of the package (e.g., "The four basic arithmetic operations as a package").
    • Dependencies added via uv add are automatically managed in this file.

Publishing the Package

  1. Set the UV_PUBLISH_TOKEN Environment Variable: The API token must be available as an environment variable named UV_PUBLISH_TOKEN. This can be done in several ways:
    • Directly in the command line: UV_PUBLISH_TOKEN=<your_token> uv publish
    • Using export in the terminal: export UV_PUBLISH_TOKEN=<your_token>
    • Using a .env file and sourcing it (as demonstrated in the video for security reasons).
  2. Build the Package: Run uv build --no-sources to build the package. The --no-sources tag is optional.
  3. Publish the Package: Execute uv publish to upload the built package to PyPI.

Testing the Package

  1. Create a Virtual Environment: Create and activate a virtual environment using python3 -m venv <env_name> and source <env_name>/bin/activate.
  2. Install the Package: Use pip3 install <package_name> to install the published package from PyPI.
  3. Import and Use the Package: Create a Python script to import and use the functions from the installed package.

Deleting the Package

  1. Navigate to Project Settings: On PyPI, go to the project's "Manage" page, then "Settings."
  2. Delete the Project: Scroll down to the "Delete" section, check the confirmation boxes, and enter the project name to permanently delete the package.

Conclusion

The video provides a practical guide to publishing Python packages using UV, emphasizing its simplicity and speed. It covers the essential steps from setting up the environment and creating a package project to building, publishing, and testing the package. The use of UV and pyproject.toml streamlines the process, making it easier for developers to share their code on PyPI. The video also demonstrates how to delete a package from PyPI, which is useful for test projects.

AI summaries can miss context or contain errors. Check important details against the original video.

Go a little deeper.

Have a question about this video? Load its transcript to open the video chat.