Publishing a Python Distribution Package in a Repository: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 52: Line 52:


==Upload the Distribution Archives==
==Upload the Distribution Archives==
Create an account, if you don't have one, for https://test.pypi.org/ at https://test.pypi.org/account/register/


<font color=darkkhaki>TODO: https://packaging.python.org/en/latest/tutorials/packaging-projects/#uploading-the-distribution-archives</font>
Create an API token here: https://test.pypi.org/manage/account/#api-tokens
 
Install <code>[[Twine|twine]]</code> as described here: {{Internal|Twine#Installation|<tt>twine</tt> Installation}}

Revision as of 02:13, 9 April 2024

External

Internal

Overview

Procedure

Create the Project Layout

The project is aimed at publishing a Python package, which contains a library. The layout and required initialization work is described here:

Python Project Layout

Since we're aiming at publishing a library, the package will probably need an __init__.py but not a __main__.py.

Choose a Build Backend

Tools like pip or build do not convert the source into a distribution packages, they're just build frontends that delegate the process to a build backend. Several build backends are available, this example uses Hatchling.

Create pyproject.toml

More details:

pyproject.toml

Create the Distribution Archives

./.venv/bin/python -m pip install --upgrade build

Run the following command from the directory where the pyproject.toml is located:

./.venv/bin/python -m build

* Creating isolated environment: venv+pip...
* Installing packages in isolated environment:
  - hatchling
* Getting build dependencies for sdist...
* Building sdist...
* Building wheel from sdist
* Creating isolated environment: venv+pip...
* Installing packages in isolated environment:
  - hatchling
* Getting build dependencies for wheel...
* Building wheel...
Successfully built ofddi-0.1.0.tar.gz and ofddi-0.1.0-py3-none-any.whl

The files are placed under the dist directory.

The tar.gz while is the source distribution, while the .whl is a built distribution.

Upload the Distribution Archives

Create an account, if you don't have one, for https://test.pypi.org/ at https://test.pypi.org/account/register/

Create an API token here: https://test.pypi.org/manage/account/#api-tokens

Install twine as described here:

twine Installation