Open laptop
Image credit: Clément Hélardot

Creating an OSS Python Library

I’ve decided to create an open-source Python library that is hosted on PyPi and can be installed using the pip install command. And since it’ll be “out there in the open,” it’ll have to be as polished and easy to use as I can possibly make it.

Now, an obvious question would be: why? Well, I see this as the next step in my journey to become a better Python programmer. But this particular package that I have in mind also solves a specific problem that I’m encountering in several of my projects.

So creating this Python library and making it available on PyPi “scratches a real itch” and encourages me to polish my code, write more and better tests, write more and better documentation, and much more.

The most important part of this exercise is obviously to create a useful Python library, and I think I can check that box. The purpose of this particular library is to aggregate several communications methods behind a common API. The library allows one to call a single function to send messages via several channels (e.g. SMS, email, Twitter, Slack). Messages can go out to one or more channels simultaneously and to several different recipients.

My use case is that I have several Raspberry PIs running different applications. At regular intervals, they “phone home” to let me know the status of this or that. And rather than writing several custom versions of essentially the same code, a better solution would be to have all this functionality in a single library that I can simply import when needed.

However, since not all of my applications and Raspberry PIs need exactly the same communication methods or have all the same connections, and so on, I’ve designed this library to be configured via .ini files so that the library can figure out how messages can be sent. This allows the function call from the main application to always be the same (e.g. send_message(msg, **attribs)).

There are, of course, existing libraries out there that do similar things. In this particular case, there’s a very nice library called “Notifiers” which does virtually the same thing, albeit a bit different. In fact, it does a lot more than my package, is well designed, and is well maintained.

And yes, I could have used that package. But I didn’t know about it until recently, and I also really want to learn how to create a polished Python library. However, the Notifiers library has been a great inspiration, and I’ve spent quite a bit of time reading its code and studying how problems were solved.

Another important part of this exercise is to learn how to create Python packages and make them available on PyPi. This turns out to be non-trivial. In fact, there are quite a few and sometimes bewildering steps in the publishing workflow, and I’ve had to learn many new tools and processes.

So far, there have been quite a few challenges and lots of … uhm … “learning opportunities.” But I’m making steady progress! My library — f451-comms — has not made it to PyPi yet, but it’s up on GitHub, and I’m slowly moving towards publishing the first version. 🤓

— // —

P.S. I’m using the “Hypermodern Python Cookiecutter” template for my project. It is a well-documented template and lists many tools that can help build better applications. However, the recommended workflow has many steps, and following them is (often) not trivial. At least, it was a steep learning curve for me.

P.P.S. I’m also reading the book “Publishing Python Packages” by Dane Hillard and am learning more about the publishing process.

P.P.P.S. There’s a great tutorial on publishing Python packages on Real Python. It’s a bit simpler and is also a good starting point.