Published on

Python tip N3 : Create a Python package and publish it with only one file

Authors
Merge-python-dictionaries

In this small blog post, I will share my tips and what I learned to quickly make your python package, and publish it with only one file.

Useful resources on this topic

This post is mainly inspired by Simon Willison blog post on this topic and the well known youtuber Arjan Codes video on this subject

Structure of the package

It's better if you can use a template to generate a skeleton of your package and its most important files. You can use this one.

Here is how a simple cli package might looks like

package-structure

Files needed to build the package

You only need pyproject.toml. The mandatory table in this file is the project table. In the project table, the mandatory names are the name and the version. Here is how it might look like for a Python CLI package.

[project]
name = "py-like-wc"
version = "0.2"
description = "python wc tool"
readme = "README.md"
authors = [{ name = "Ismail Tlemcani" }]
license = { text = "Apache-2.0" }
requires-python = ">=3.9"
classifiers = ["License :: OSI Approved :: Apache Software License"]
dependencies = []

Build the package

You can build the package by using only the build package in the following way. This is now the standard way to build python packages.

python -m build

This command will create 2 files : a whl file and a tar file

python-dist-folder

Publish the package

To publish the package, you can use the twine package. The command to do this is the following command

twine upload dist/*

By default, this command will upload your package to the official PyPi repository. If you want to upload the package to another repository, you can define a custom configuration in your .pypirc file. This file is usually in your HOME directory

[testpypi]
repository: https://test.pypi.org/legacy/
username: my_username
password: my_pypi_token

You can see more details on how to define a .pypirc file here

You can then upload the package to the repository you defined by using the section title (testpypi in the example above) with the following command

twine upload -r testpypi dist/*

When uploaded, you can see your package in the repository in the url that the twine command provided in your console output

upload-output

Update the package

You simply have to change the version, and then uplaod again