Pragmatism in the real world

Parsing command line arguments in Python

This is one of those posts that I write so that I can look it up again as this information is all over the Internet, but it took me a little while to find what I was looking for. To accept command line arguments in a Python script, use the argparse package: import argparse You can then instantiate a parser and add arguments you want to accept: parser = argparse.ArgumentParser(description="Convert a JSON array to JSONL… continue reading.

Defining Python dependencies at the top of the file

Earlier in the year, I wrote about updating Flickr metadata using Python. For this script to work, I needed to install the flickrapi package first. I recently came across PEP 723 – Inline script metadata that makes this much easier for single file scripts like my sync-flickr-dates script. Essentially, we can now put a special comment block at the top of the file, before our import statements like this: # /// script # requires-python =… continue reading.

Installing PyMuPDF on a prerelease Python

I recently installed Python 3.13.0 rc2 to test rst2pdf against it and found that I couldn't install PyMyPDF which is required for the tests. $ pip install pymupdf … This is because for a pre-release version, binary wheels are not provided to PyPI for the mupdf dependency which is written in C++. Hence, the compiler needs the headers. On my Mac, I had the CPATH environment variable set to /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include. For mypdf to compile, I… continue reading.

Updating Flickr metadata using Python

I usually use my rodeo app to upload photos to Flickr, but for various reasons, I recently uploaded some photos directly via Flickr itself. One feature of rodeo that I really like is that it sets the date posted to be the same as date taken which means that they are ordered correctly in my photo stream. As this doesn't automatically, I knocked up a Python script to fix these photos for me. The key… continue reading.

Quick script to (re)create my python virtualenv

When working on rst2pdf, I use pyenv as I've written about before. Recently, I've found myself needing to recreate virtualenvs for various Python versions and then recreate them to easily reset the list of packages installed into them via pip. To my my life easier, I created a script that I can run from the command line: $ newenv rst2pdf-dev-py3.9 3.9.5 This creates a new virtualenv called rst2pdf-dev-py3.9 from Python version 3.9.5, upgrade pip and… continue reading.

Cannot find zlib headers on macOS 10.15 Catalina

My new MacBook Pro 16" comes with Catalina and while setting up Python to develop rst2pdf, I discovered that Pillow wouldn't install. Running setup.py install for pillow … error ERROR: Command errored out with exit status 1: Looking through the long list of red text I came across: The headers or library files could not be found for zlib, a required dependency when compiling Pillow from source. Aha! The way this was solved on the… continue reading.

Creating virtual environments with Pyenv

rst2pdf is a Python 2 application that we're making compatible with Python 3. When developing Python applications, I've found it useful to be able to switch python versions easily and also set up clean environments to work in. To do this, I currently use pyenv. This is how I set it up: Install Pyenv On my Mac, I install pyenv & its sister project pyenv-virtualenv with Homebrew: $ brew install readline xz $ brew install… continue reading.

Step debugging sphinx-build in PyCharm

I've been trying to solve an issue with the rst2pdf Sphinx extension which allows you to use rst2pdf to create a PDF of your Sphinx documentation rather than using Sphinx's default pdfTeX builder. In order to understand what was happening, I really wanted to inspect variable at certain stages of the process, which is easiest with a step debugger. As I already use PhpStorm for step debugging PHP, I fired up PyCharm and worked out… continue reading.