Publishing a command line app to PyPI using poetry

Alfurquan Zahedi
3 min readMar 24, 2024

--

This article is a continuation of my previous article where we built a simple expense tracker command line app using python, typer and poetry. In this article we will use the project built in the previous article and package and publish it to PyPI.

Packaging in python

Python is famous for coming with batteries included, and many sophisticated capabilities are available in the standard library. However, to unlock the full potential of the language, we should also take advantage of the community contributions at PyPI: the Python Packaging Index.

PyPI, typically pronounced pie-pee-eye, is a repository containing several hundred thousand packages. These range from trivial Hello, World implementations to advanced deep learning libraries. In this article, you’ll learn how to upload own package to PyPI. Publishing a project is easier than it used to be. Yet, there are still a few steps involved. We will be using poetry to build and publish our package to PyPI.

Preparing our package

We take the project we built in the last article and prepare it for packaging. We take a look at the pyproject.toml file and update the package name to be a unique name so that we can publish our package to PyPI without conflicting with other packages.

[tool.poetry]
name = "expense-tracking-cli" #This name has to be unique
version = "0.1.0" #This is the version of the package
description = ""
authors = ["Alfurquan Zahedi <zahedialfurquan20@gmail.com>"]
readme = "README.md"
packages = [{include = "expense_tracker"}]

[tool.poetry.scripts]
expense = "expense_tracker.cli:app"

[tool.poetry.dependencies]
python = "^3.10"
tinydb = "4.5.1"
typer = "0.3.2"
rich = "10.7.0"

[tool.poetry.dev-dependencies]
pytest = "^5.2"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

Once we have the package name set and version specified, we can go ahead with the next steps.

Setting up PyPI account

We go to PyPI and create off an account, if we do not have one. We make sure to setup two factor authentication on the account. We can follow the steps here to set up 2FA for our PyPI account. Once we have a PyPI account set with 2FA, we go ahead and create API tokens to upload packages following steps from here.

Setting up PyPI credentials with poetry

One we have the PyPI account set and API tokens we go ahead and configure the credentials for poetry to use.

We run these commands to set the creds for poetry.

poetry config pypi-token.pypi <my-token>

Building the package

Next up, we go ahead and build our package using poetry.

We run this command from within the project directory

PS C:\expense-tracker>poetry build

This command goes ahead and builds out our package. It generates two files one tar file and other whl file in the dist/ folder.

Publishing our package

In order to publish our package, we simply run this command from within the project directory.

PS C:\expense-tracker>poetry publish
Publishing expense-tracking-cli (0.1.0) to PyPI
- Uploading expense_tracking_cli-0.1.0-py3-none-any.whl 0%
- Uploading expense_tracking_cli-0.1.0-py3-none-any.whl 68%
- Uploading expense_tracking_cli-0.1.0-py3-none-any.whl 100%
- Uploading expense_tracking_cli-0.1.0.tar.gz 0%
- Uploading expense_tracking_cli-0.1.0.tar.gz 100%

This will upload our package to PyPI. In addition to aiding in building and publishing, Poetry can help us earlier in the process. Poetry can help us start a new project with the new command. It also supports working with virtual environments. See Poetry’s documentation for all the details.

Cool! So we have our package published to PyPI and we can share it and use it any project we want.

With this we come to the end of the two part series on building a simple command line application using python and typer, using poetry to package and publish our application.

References

P.S. If you enjoyed this article and want more like these, please clap ❤ and share with friends that may need it, it’s good karma.

--

--

Alfurquan Zahedi

Software engineer (Nodejs,Reactjs,Python,Android,.NET) A computer science enthusiast with love towards coding and application development.