I published my first package to PyPi
As long-time readers of my blog may know, I like gathering environmental data like temperature and pressure. And some time ago I started thinking, if I am able to gather such data over a timespan, maybe I could use it for local forecasting of weather? I know that real forecasting requires a lot of data and massive computation, but maybe there is something simple for short-term forecasts?
A physical Zambretti Forecaster, image taken from Wikipedia, copyright CC BY-SA 4.0
A quick startpaging* presented me with the Zambretti algorithm, a relatively simple way to forecast the weather for the next hours, using the current temperature and a history of changes in atmospheric pressure. Looked like something perfect for my basic use case.
*Startpage is currently my favourite web search engine.
Now, it looks simple and useful, surely somebody already implemented it in Python!
And turns out that no, there had been no Python implementation of the Zambretti algorithm on PyPi. And don’t call me Shirley.
I dropped the topic for a few weeks, but one day I had an idea… if nobody made a Python package for it, maybe I could do it? And publish it? That would be cool, a learning experience, and having my own package on PyPi would be another step in my Python development ladder.
The code
The code can be found on GitHub - zambretti-py. I usually prefer Codeberg, but this time I went with GH, mostly for the easily available Actions.
And so I sat down and started writing my own package. I decided to start small and simple, without any dependencies, using only what is available in the Python Standard Library.
My main source of inspiration was this repo, I took some of the code snippets from it and converted them into Python.
Making it into a package for anyone to use raised for me some interesting questions regarding the project structure and imports. I went with imports in the __init__.py
file to simplify importing when using the package.
The biggest issue with this code is that it can be better. Showing your work publicly is never easy, and impostor syndrome is strong with this one. And on top of that are the thoughts of “you are called a senior developer at work, how can you now know how to make it better?!“. So thank you to all my Fediverse friends who encouraged me to actually carry on and publish it.
And I can always make it better with time, right? The package is currently at version 0.0.4, there is still a long way to reach 1.0.0.
I have plans on how to improve it, probably at some point I will drop the “uses only the standard library” idea. I’m thinking of using Pydantic
for data storage and validation, Pydantic
has been one of my favourite Python modules lately.
I would love to see somebody made a PR with a suggested improvement, it’s always good to see other people’s ideas.
Publishing on PyPi
I started my plan for publishing on PyPi with asking the Fediverse people for tips on how to do it, and kindly enough, I received a lot of replies with great feedback. This is what I really enjoy about Fedi, people there are not only knowledgeable, but also helpful <3
You can read the conversation in full here.
As with the code I decided to start slow, small, and simple.
I have to praise the documentation available at packaging.python.org, it is clear, well written, and provides good examples. Exactly what good documentation should have.
My main source of knowledge was this tutorial on packaging which is a concise version of a more in-depth documentation.
Going along the tutorial I modified the structure of the project, added the required pyproject.toml
file, and filled it with the metadata.
For testing, I registered at Test PyPi, created a token, and with a imaginary drumroll, I published zambretti-py
to the test repository with a single command in the terminal. And it worked! I was amazed how simple and straightforward the whole process is. Publishing to a test repo allowed to straighten some things with naming and metadata.
Going to Prod
After resolving the minor issues that I found after publishing to the test env, and after another round of code refactoring (you can never have enough refactoring), I decided to jump on the deep end and go with Prod, the real PyPi.
The publishing process was virtually the same, I registered at pypi.org, set up a token and used the same commands, just without the bits defining the testing repository.
Alas! zambretti-py
is now live! Other Python developers can now find it and use it in their projects, I made a tiny contribution to the Python ecosystem and that felt and still feels great.
Switching to GitHub Actions
After publishing I manually I looked for ways to streamline the publishing process and I found that GitHub provides a ready to use Action that publishes packages to PyPi. The only required configuration is setting the secret for the PyPi token. The action is run on every creation of a release on GH.
I’m used to using GH Actions and releases at work, but having to use the releases mechanism for my own project is something new, and another learning opportunity.
Next steps
The closest next step is to improve the code, obviously :)
In the conversation in the Fediverse about publishing, many people mentioned something called Trusted Publishers and this is something I want to take a closer look.
Bottom Text
Even though it is just a small and simple package, the whole process has been a great learning opportunity for me. Packaging in Python is something totally new to me, and also I had to refresh my knowledge on GitHub Actions and releases.
I’m hoping I will continue this journey, and learn even more new things as I maintain zambretti-py
.
And I am happy to be a tiny part of the FOSS contributors community.
If you are also thinking of publishing your package to PyPi, do it! It can be scary at the beginning, but all in all, it is something that anyone can do easily, and there is nothing stopping you.
I would like to again thank everyone on the Fediverse who gave me ideas, feedback and nice words, and answered my questions, you people rock.
P.S. I wrote most of this blog post in Neovim running in tmux. I’m really getting into those two tools, and using them instead of VSCode sparks joy in me. Probably soon I will make a post specifically on how I am using them and how do my configuration files look like.
Thanks for reading!
If you enjoyed this post, please consider helping me make new projects by supporting me on the following crowdfunding sites: