4 Tools to Write Better Python
PEP8, Pylint, IPython, and isort
When I first started writing Python I made so many mistakes. It was frustrating. I first learned programming in Ruby and although becoming fluent in Python wasn’t earth-shattering, it was a bit annoying.
I fought against the general convention of the language and “The Zen of Python”. Each syntax error made me go: “What a silly rule. Why?”
Indentation as syntax felt weird. It made the code look ugly and trail off to the right. Shorthand ways of writing methods and clever one-liners I was used to were invalid now and had to be re-written in a way that seemed alien.
And then… everything changed.
Well, sort of. Not all at once, but slowly things changed. The syntax started to look less foreign and I began to mildly enjoy writing Python. The things that annoyed me eventually fell into the background.
Glaring dislikes like indentation-as-syntax are still there. I still don’t appreciate some of the overall syntax decisions, but writing Python has become less frustrating.
The trials and tribulations of getting used to a new language can be assuaged through the judicious application of tools. Learn the style guide. Lint your code. Get comfortable.
I’ve put together a list of tools that helped me on my path to becoming a better Python developer. Hopefully, tools like this will ease the pain and point out areas for improvement early on.
1. PEP8
First up and most importantly is the Python style guide. This is not necessarily a tool you run, but a tool you reference. While developing, you’ll undoubtedly run into scenarios where you think: “How should I write this?”
Your first reaction should be to check the style guide. Referencing the style guide first cements the optimal design choices in your mind and makes writing a similar method or function the next time even quicker.
There are also two additional steps you can take to really drive the style home early on:
- Don’t just reference it when you run into a problem, read it. Investigate how the language expects certain data structures to be formatted or how functions should be properly declared.
- Use the style guide in conjunction with the next tool (Pylint) as an indicator of areas to focus on more closely. If you keep violating certain rules every time then go over that section of the style guide again.
2. Pylint
Up next is a handy tool for showing you all the errors you’ve made and conventions you’ve violated in your code. Pylint is a command-line tool for linting Python files.
If you aren’t familiar at all with linting, there is a fantastic article by Colby Fayock called “What Is Linting?”
Linting boils down to checking your code for errors and checking it against a style guide. In this case, installing Pylint and running it against your Python files produces a list of issues that need to be addressed. This includes things like line length, function formatting, variable naming, etc.
Each time you write a new Python file or update an existing one, you should run Pylint and check for issues. This can be automated by other third-party tools or simply built into your existing IDE by a plugin.
Over time, you will find that you produce fewer issues and have fewer things to fix after writing some code.
3. IPython
IPython is a drop-in replacement for the existing Python REPL with one crucial improvement — syntax highlighting!
Much like the Pry gem for Ruby, IPython gives you colorized output, automatic indentation, and interactive syntax highlighting.
Using IPython is super easy too, all you need to do is install and run ipython
just like you would the regular Python REPL:
sudo apt-get install ipython
ipython
Using this drop-in replacement gives you better feedback as you’re testing code and developing short snippets.
You’ll be able to see the syntax more clearly as you type and you’ll get helpful tweaks like auto-indent to prevent having to type code all over again if you miss a tab.
4. isort
This isn’t the most useful tool on the planet at first, but one that saves you from monotonous work over and over again.
is
ort stands for “import sort” and it does exactly what it sounds like. This tool will accept a Python file and sort all of the import
statements within it in alphabetical order.
At first glance, it seems like this might not be necessary if you’re only working with a few files and a small set of import statements. When the code base grows and more imports are introduced, it will quickly become annoying keeping them in alphabetical order.
You might toss an import statement in at the bottom of the list to test something. It turns out that that thing you were testing works great and you want to commit the code. The next thing you know, you’re failing Pylint because your imports are not alphabetical.
Use isort
and never alphabetize by hand again.
This can be installed via pip
or through apt
depending on your disto:
pip install isort
sudo apt install isort
There is also a handy way to add isort
as a Git hook so it is run automatically before you even commit anything. For more information on configuring this, check out the official docs.
Conclusion
Thanks for reading! I hope some of these tools will enhance your development experience with Python and make getting used to the syntax and style easier.