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

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(22 intermediate revisions by the same user not shown)
Line 12: Line 12:


Since we're aiming at publishing a library, the package will probably need an <code>[[Python_Language_Modularization#init_.py|__init__.py]]</code> but not a <code>[[Python_Project_Layout#Add_main_.py|__main__.py]]</code>.
Since we're aiming at publishing a library, the package will probably need an <code>[[Python_Language_Modularization#init_.py|__init__.py]]</code> but not a <code>[[Python_Project_Layout#Add_main_.py|__main__.py]]</code>.
==Choose a Build Backend==
Tools like <code>[[pip|pip]]</code> or <code>build</code> do not convert the source into a [[Python_Language_Modularization#Distribution_Package|distribution packages]], they're just [[Python_Language_Modularization#Build_Frontend|build frontends]] that delegate the process to a [[Python_Language_Modularization#Build_Backend|build backend]]. Several build backends are available, this example uses [[Hatch#Hatchling|Hatchling]].


==Create <tt>pyproject.toml</tt>==
==Create <tt>pyproject.toml</tt>==


More details: {{Internal|Pyproject.toml#Overview|<tt>pyproject.toml</tt>}}
More details: {{Internal|Pyproject.toml#Overview|<tt>pyproject.toml</tt>}}
==Create the Distribution Archives==
<syntaxhighlight lang='bash'>
./.venv/bin/python -m pip install --upgrade build
</syntaxhighlight>
Run the following command from the directory where the <code>pyproject.toml</code> is located:
<syntaxhighlight lang='bash'>
./.venv/bin/python -m build
</syntaxhighlight>
<font size=-2>
* 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
</font>
The files are placed under the <code>[[Python_Project_Layout#Add_main_.py|dist]]</code> directory.
<span id='Built_Distribution'></span>The <code>tar.gz</code> while is the source distribution, while the <code>.whl</code> is a [[Python_Language_Modularization#Wheel|built distribution]].
==Upload the Distribution Archives==
===<tt>test.pypi.org</tt>===
Optional, for testing, 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 <code>[[Twine|twine]]</code> as described here: {{Internal|Twine#Installation|<tt>twine</tt> Installation}}
Install the archives under <code>dist</code>:
<syntaxhighlight lang='bash'>
.venv/bin/python -m twine upload --repository testpypi dist/*
</syntaxhighlight>
Use <code>__token__</code> as username and the API token as password. Alternatively, the program may ask directly for the API Token. Alternatively, configure it in the file: {{Internal|Twine#Set_up_API_Token|<tt>twine</tt> &#124; Set up API Token}}
Once uploaded, they can be viewed at: <code>https&#58;//test.pypi.org/project/<project-name>/<version>/</code> (<code>https://test.pypi.org/project/ofddi/0.1.0/</code>)
To verify it works, you can use <code>pip</code> to download it.
===<tt>pypi.org</tt>===
Create an account, if you don't have one, for https://pypi.org
Create an API token.
Install the archives under <code>dist</code>:
<syntaxhighlight lang='bash'>
.venv/bin/python -m twine upload dist/*
</syntaxhighlight>
Use <code>__token__</code> as username and the API token as password. Alternatively, the program may ask directly for the API Token. Alternatively, configure it in the file: {{Internal|Twine#Set_up_API_Token|<tt>twine</tt> &#124; Set up API Token}}
Once uploaded, they can be viewed at: <code>https&#58;//pypi.org/project/<project-name>/<version>/</code> (<code>https://pypi.org/project/ofddi/0.1.0/</code>)
To verify it works, you can use <code>pip</code> to download it.
==Upload from GitHub==
<font color=darkkhaki>https://pypi.org/manage/account/publishing/</font>

Latest revision as of 02:40, 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

test.pypi.org

Optional, for testing, 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

Install the archives under dist:

.venv/bin/python -m twine upload --repository testpypi dist/*

Use __token__ as username and the API token as password. Alternatively, the program may ask directly for the API Token. Alternatively, configure it in the file:

twine | Set up API Token

Once uploaded, they can be viewed at: https://test.pypi.org/project/<project-name>/<version>/ (https://test.pypi.org/project/ofddi/0.1.0/)

To verify it works, you can use pip to download it.

pypi.org

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

Create an API token.

Install the archives under dist:

.venv/bin/python -m twine upload dist/*

Use __token__ as username and the API token as password. Alternatively, the program may ask directly for the API Token. Alternatively, configure it in the file:

twine | Set up API Token

Once uploaded, they can be viewed at: https://pypi.org/project/<project-name>/<version>/ (https://pypi.org/project/ofddi/0.1.0/)

To verify it works, you can use pip to download it.

Upload from GitHub

https://pypi.org/manage/account/publishing/