If, however, you see 2.
something (or worse, 2.
6 or lower), I highly recommend taking a further dive into your system and determine if your development environment is at-risk of future maintenance headaches and vulnerabilities.
Photo by Hush Naidoo on UnsplashDetermining the causeNow, just because the system reports version 2.
7 or similar, doesn’t necessarily mean that’s the version your Python programs are using.
For example, if you are using virtual environments or Anaconda to localize your Python executable, you might still be ok if you are using a newer version of the language for all of your projects.
If any of these projects are still pointing at old versions of Python, then it would be wise to work on upgrading the system (more on that below).
If virtual environments and Anaconda are meaningless to you, then you are likely just using the default version of Python which came with the OS.
If that’s the case, then I highly recommend working on updating your environment, either locally to a project, or globally.
Photo by Fancycrave on UnsplashFixing the problemIf you’re still reading this, you are likely using an older version of Python and are looking for help on upgrading.
I recommend the following general steps to update to the latest and greatest:Installing a newer version of PythonUpgrading all dependent packagesUpdate code to be Python 3 compatibleTest the updatesDeploy the finalized codeLet’s take a deeper dive into each step!Installing a newer Python versionThere are a few different options to actually install the latest version of Python on your system.
A couple of these include using virtual project localization, such as Anaconda and virtual environments.
The official Anaconda documentation has a great guide on installing and using it here.
Virtual environments can also be installed using the following guide, but note that you will need to download and install the desired version of Python to your system first.
More information on downloading and installing Python can be found here.
Of course, you can choose to simply install a newer version of Python system-wide and forget about project localization, though I recommend using one of the above methods to ensure dependencies are specific to each project.
Another option is to use Docker containers and run the application inside a custom Python container.
While I love containerizing projects, it might not necessarily be the easiest or most logical and practical solution.
Upgrading all dependent packagesSince many Python packages are beginning to drop support for 2.
7, it’s entirely possible that a project expects older versions of dependent packages to function properly.
If your project’s dependencies are listed in a requirements.
txt file in the directory (which I would strongly recommend doing if they aren’t already), it would be wise to examine all of the listed packages and attempt to upgrade them if possible.
This ensures your dependencies stay up-to-date with the latest version of Python, even if everything appeared current with Python 2.
Update code to be Python 3 compatibleLuckily, there isn’t too much of a difference between the Python 2 and 3 code-bases, but certain commands can make working code instantly fail on newer versions.
One of the biggest examples is the print command.
With Python 2, print is a special statement, allowing us to writeprint "Hello world!"The behavior changed with Python 3.
The print statement is now an actual function, so that same print statement above must now be written asprint("Hello World!")Another important issue is the handling of division for integers.
In Python 2, dividing two integers will always return an integer.
For example3 / 2 = 1Despite the correct answer being 1.
5, Python 2 sees this as an integer divided by an integer equals an integer, hence 1.
Python 3, however, returns a float if applicable in this situation.
The same statement would evaluate to3 / 2 = 1.
5This can have a very significant impact on results, especially for data science projects that rely on mathematical operations.
While these are just a few of many deltas between versions 2 and 3, they are generally the biggest culprit for errors after an upgrade.
A list of some of the other major differences from a usage standpoint can be found in this blog post.
Fortunately for us, some useful tools were created to aid the transition from 2 to 3.
One such tool is the aptly named 2to3 which comes with most Python installations.
Running this program on a particular file will output the changes that need to be made for it to be compatible with Python 3.
While this program is a valuable resource for converting code, it isn’t perfect, so I recommend reviewing the changes to ensure everything is logical and a necessary update.
Test the updatesOnce Python and the program’s dependencies have been updated along with the code, it’s time to test the changes to ensure everything works as expected for the newer version.
Hopefully your project contains tests of some sort (if not, I highly recommend writing unit tests at the very least to ensure your code works as expected) which you can run with Python 3 to validate the code.
After running a testing suite (if applicable), run some live tests that would simulate a production use-case.
Hopefully this production test traverses through most (if not all) of the code base to verify everything works as expected without issue.
If all looks rosy, your code is [likely] ready for prime-time!Deploy the finalized codeIf you made it this far, congratulations!.You should have successfully converted your project from Python 2 to 3 and ensured that your code is future-proof.
At this point, it is probably safe to push the changes to production environments, update any upstream repositories, and verify all development environments are using the latest version of Python.
Your code should now be able to take advantage of the vast number of improvements Python 3 offers over its predecessor, while also creating a stable code-base that will be supported for many more years to come.
Photo by Alice Achterhof on UnsplashFinishing touchesIf you followed all of the steps above, you should now solely be using Python 3 for your projects.
As mentioned throughout the article, failure to upgrade brings the potential of issues moving forward.
Sometimes upgrading isn’t always possible, such as the case of production systems requiring a particular OS or system-wide Python version.
A couple different solutions would be to use virtual environments or Anaconda to install a different version of Python, or containerize the application with Docker so it will use the expected version of Python, regardless of which OS you are running on.
There’s no telling what will happen over the next several months as the end of 2.
By proactively updating your project, you can avoid any potential pitfalls after the milestone.
7 was the first version used by many Python developers around the world and has been a major vessel for the explosion in deep and machine learning projects over the past few years.
But, even as the greatest of fires die off after time, so too do beasts of the software world.
Thanks for the memories 2.
May the future of 3 be bright and prosperous.