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, andpyproject.tomlfile, 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
- Install UV: The video demonstrates installing UV using
pip install uvor a Linux package manager (e.g.,sudo pacman -S python-uvon Arch Linux). - Create a PyPI Account: Register an account on pypi.org and verify the email address.
- 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
- 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. - 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.pycontains simple arithmetic functions (add,sub,mole,diff). - Populate the
__init__.pyfile to make the functions accessible when the package is imported. The example usesfrom .operations import add, sub, mole, diffand defines__all__ = ['add', 'sub', 'mole', 'diff']. - Example
operations.pycontent:
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
- Modify Metadata: Edit the
pyproject.tomlfile 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 addare automatically managed in this file.
Publishing the Package
- Set the
UV_PUBLISH_TOKENEnvironment Variable: The API token must be available as an environment variable namedUV_PUBLISH_TOKEN. This can be done in several ways:- Directly in the command line:
UV_PUBLISH_TOKEN=<your_token> uv publish - Using
exportin the terminal:export UV_PUBLISH_TOKEN=<your_token> - Using a
.envfile and sourcing it (as demonstrated in the video for security reasons).
- Directly in the command line:
- Build the Package: Run
uv build --no-sourcesto build the package. The--no-sourcestag is optional. - Publish the Package: Execute
uv publishto upload the built package to PyPI.
Testing the Package
- Create a Virtual Environment: Create and activate a virtual environment using
python3 -m venv <env_name>andsource <env_name>/bin/activate. - Install the Package: Use
pip3 install <package_name>to install the published package from PyPI. - Import and Use the Package: Create a Python script to import and use the functions from the installed package.
Deleting the Package
- Navigate to Project Settings: On PyPI, go to the project's "Manage" page, then "Settings."
- 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.





