How to Upgrade DevOps Code to Python 3

Python 2 is going away! It’s time to upgrade.

You shouldn’t run anything in prod that’s not actively supported. If there are security flaws you won’t have a sure path to remediation. Start the upgrade now so you have time to finish before support ends.

In DevOps you’re not usually writing much raw Python. A helper lambda function. A little boto3 script. If you’re writing lots of code, you’re probably making a mistake and you should be looking for an existing tool that already implements whatever you’re doing (terraform, troposphere, Ansible, Salt, paramiko, whatever).

Because of that, migrating DevOps code to Python 3 is usually easy. There are guides and a conversion tool. I usually just switch my interpreter to 3 and fix errors until there aren’t any more. A few old features have been replaced with new ones that are worth adopting. Here are highlights from the easy migrations I’ve done (keep reading for the one that wasn’t easy):

  • Virtual environments are now in core as venv. You don’t need to install virtualenv anymore.
  • basestring was replaced with str.
  • Use the print() function instead of the print statement. Printing output isn’t usually ideal, and this may be a good opportunity to upgrade to logging.
  • ConfigParser was renamed to configparser (to match the Python convention).
  • mock is now in core as unittest.mock.
  • The new f-strings are awesome. format() and the other string formatters still work, so f-strings aren’t a migration requirement, but they make cleaner code and I recommend switching to them.

Like always, lint your code before you run it!

One migration I got into wasn’t simple: I’d bodged together a script from snippets of a library that used Python 2 sockets to implement ping so I could watch the gears turn inside the black boxes of AWS Security Groups. I got into the weeds of unicode and not-unicode strings and then decided to just live with Python 2.

If that story reminded you of any of your own code, I recommend you don’t try to migrate that code. Look for a tool that already implements whatever you’re trying to do, find a way not to need to do whatever you were doing, something. In my case, that script wasn’t part of delivering product. I was just hacking around. I finished my experiments and deleted the script.

Happy upgrading!


Need more than just this article? We’re available to consult.

You might also want to check out these related articles: