· #hacks · #xdd
Back in 2013, I built a small website named [x] driven development to scratch a personal itch. Based on the commit logs, I hadn’t touched the site for the past four years!
So I decided to dust off the repository and implement the sole backlog item on the list: enforce HTTPS. While implementing this item was a quick two lines of redirect code in the Apache
.htaccess file, I wasn’t surprised to come up against some bit rot.
The bit rot was caused mainly by two events that occurred since 2016. First, Python 2 finally reached EOL. Second, the Dreamhost servers where the site is hosted were upgraded from Ubuntu Trusty Tahr to Bionic Beaver. Thus, I had to first update my code to run under Python 3 and then setup a new Python 3 based environment on Dreamhost.
The first step - updating the code - was pretty easy. I only had to change a few lines of code that dealt with file I/O.
The second step was a bit more interesting. Typically, following best practice, I setup a Python virtual environment for each project. But creating one on Dreamhost’s Bionic OS failed:
$ python3 -m venv xdd The virtual environment was not created successfully because ensurepip is not available. On Debian/Ubuntu systems, you need to install the python3-venv package using the following command. apt-get install python3-venv You may need to use sudo with that command. After installing the python3-venv package, recreate your virtual environment. Failing command: ['/home/antrix/local/virtualenvs/xdd/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']
This being a shared environment, I couldn’t just
sudo apt-get install anything.
A bit of googling brought up articles that recommended ignoring the system installation of Python and compiling your own Python 3 build. Honestly, that seemed like overkill for what I wanted to achieve. But I did learn something new that turned out to be the simplest possible solution.
The virtual environment construct is so popular in the Python community that starting in Python 3, venv became part of the standard library. Thus, you don’t need to install the third-party virtualenv package any more. Any Python 3 installation allows you to work with virtual environments out of the box. Well, any sane Python 3 installation.
Based on this, I had assumed that the
virtualenv package was not even available for Python 3. But it actually is! Thus, getting access to it is as simple as:
$ pip3 install --user virtualenv Collecting virtualenv Downloading https://files.pythonhosted.org/packages/2c/a5/bb663a902f4e8e0a55e58b5b4cb6eb1460408603f50b942d756952f916c2/virtualenv-20.3.1-py2.py3-none-any.whl (5.7MB) 100% |████████████████████████████████| 5.7MB 146kB/s .... Installing collected packages: typing-extensions, zipp, importlib-metadata, distlib, six, appdirs, filelock, importlib-resources, virtualenv Successfully installed appdirs-1.4.4 distlib-0.3.1 filelock-3.0.12 importlib-metadata-3.4.0 importlib-resources-5.0.0 six-1.15.0 typing-extensions-188.8.131.52 virtualenv-20.3.1 zipp-3.4.0
After which, creating a virtual environment is similar to the way we used to do it with Python 2:
$ python3 -m virtualenv xdd created virtual environment CPython3.6.9.final.0-64 in 3171ms
With that squared away, I managed to bring the xdd codebase into 2021. I hope it doesn’t need maintenance at least until 2025!