Hey guys, I hate to brag but I’m officially a published author! That’s right I published my Python ez-txt2html converter to PyPi, and it was easy! Here’s how I did it.
First off, for simplicity sake, I did away with my src directory and instead moved all my py code into a directory named for my project (/ez-txt2html). Creating a directory named for your project is important when creating a build to upload to PyPi.
After some research into the process I decided to use hatch as the build backend, so I needed the package installed,which was as simple as pip install hatch
Next up I added some build related details to my pyproject.toml which included some version info, general project details as well as some instructions on how to create a build. Most importantly was adding:
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
This directs the build function to use hatch to create a build of your package. In addition, I was receiving an error when trying to build that required I define what package should be targeted. This was done by adding the following to the pyproject.toml file:
[tool.hatch.build.targets.wheel]
packages = ["ez-txt2html"]
This is essentially just pointing to the newly created package directory. The result was the creation the /dist directory which contained the .whl and .tar.gz binary files which are what eventually get added to PyPi as your packages.
From there, I needed to learn a bit about PyPi or Python Package Index, which is the home for all the wonderful packages that you know if you have ever run the handy pip install
command. PyPi has a pretty quick and easy onboarding, which requires a secured account be created and, for the purposes of submitting packages from CLI, an API token be generated. This can be done in your PyPi profile. Once logg just navigate to https://pypi.org/manage/account/ and scroll down to the API tokens section. Click “Add Token” and follow the few steps to generate an API token which is your access point to uploading packages.
With all this in place, I was able to use twine to handle the package upload. First I needed to install twine, again as simple as pip install twine
. In order for twine to access my API token during the package upload process, it needed to read it from .pypirc file that contains the token info. For some that file may exist already, for me I was required to create it. Working in windows I simply used a text editor to create it in my home user directory ($HOME/.pypirc). The file contents had a TOML like format looked like this:
[pypi]
username = __token__
password = "your-PyPi-api-token"
And finally, the package could be uploaded using the command python -m twine upload --repository pypi dist/*
This uploads the contents of the project’s /dist directory to a PyPi and if all goes well, provides you with a link to your package page. Here, front and center is a handy command line that can be run on CLI. For those playing along at home run pip install ez-txt2html-bryce-seefieldt
.
If I did my job right, this will download and install ez-txt2html as an executable and importable module in your Python environment. From their you can run python -m ez-txt2html.ez_txt2html -h
to see the options for running the package from CLI. Like magic. What an accomplishment, if I do say so myself. I’ve passed this on to a couple of my devBuds and it seems to work for them NP – let me know if it works for you, or if you have any tips on how I could make this process smoother or smarter.
Top comments (0)