Some Python “Best Practices”

Python Logo As I have said before, I don’t believe their are truly “best practices”, sometimes there are “best known practices”, or “best known practices for a given context”.    But, there are often times when it is clear something is a bad practice.  And through the school of hard knocks, I’ve learned from attempting some practices that I thought were good, but turned out to be bad.  So here is what I have learned.

Program in Python 2.x, but use Python 3 Syntax

I have now read several good articles about Python 2 vs. 3, including the official wiki article, an article by one of the Python developers, an article that uses the analogy of gravity to describe the problem with going to Python 3, and an article by someone who thinks the Python 2 to Python 3 transition has been a bit of a train wreck.  It seems that in all of these articles, it is clear that Python 3 is a more elegant and clean language, but that Python 2 has all the libraries, and that due to the challenges of converting many of these libraries, even the Python organization is still using 2.x for their website…  And because of the “Theory of Constraints“, if any one library you want to use is only Python 2.x, then you cannot use Python 3.x.

So what is the best solution?  First, when you first learn basic Python, learn 3.x using PortablePython, because you aren’t working in real-world apps that need libraries.  But as soon as you get out of the infancy of programming, you should move back to Python 2.x, but write your code in a way that it will work on Python 3.x.  This way you aren’t fully perpetuating the Python 2 “gravity”, but at the same time, you can actually use the full power of Python.

In Windows, Don’t Install to C:\Program Files

Yes, I know, usually installing software to C:\Program Files or C:\Program Files (x86) is a best practice.  But honestly Python sucks at handling this, and for instance, when I tried recently to install Python into Program Files on a Windows Sever 2012 machine, to script some admin tasks, I got permission errors all over the place.  I also suspect several other problems I’ve had has been due to trying to follow a Windows best practice, but causing problems with Python.

Put Python’s Directory in the Path

In Windows, when you install Python, you have the option to put Python in the Windows Path, but it isn’t checked by default.  Change this!  If you are going to use Python at all at the command line you are going to want this!

Leave a Comment

Your email address will not be published. Required fields are marked *