Python best practices
Prepare yourself for working with multiple Python versions
Due to, amongst other things, time constraints and technical limitations, you will end up dealing with multiple Python versions on a single workstation at some point.
Install pyenv, and don’t worry about Python versions again.
The latest is not always the greatest
Before diving in and picking the latest Python version, check the documentation of the primary libraries you will use to ensure they already support the newest version. If not, choose the lowest common denominator Python release that all these libraries support.
Example: Numpy / Python compatibility matrix
Use a linter to catch common errors
I tend to use Pylint with the Google pylintrc file.
You can run Pylint from PyCharm if you want to: Running Pylint from PyCharm - PyCharm documentation.
In some cases, Pylint might flag false positives. You can turn off messages for these by putting a particular code comment before the problematic code block or in the line that triggered the issue. See Message control for more information.
Talk about code not formatting
Use an opinionated code formatter to reduce discussions on code formatting and to spend time discussing what really matters — the actual implementation. Google recommends the use of Black in their Python style guide, and it seems to be in general well-adopted.
Capture errors in development using “Python Development Mode”
Enable Python Development Mode on your local machine for additional runtime checks when working with Python. Don’t use this in a production environment.
In your IDE or terminal, set PYTHONDEVMODE=1
.
Use a dependency manager like Pip or Poetry
So far, I’ve been using pip in combination with Python virtual environments: python3 -m venv <venv-name>
.
For a future project, I will probably switch to Poetry, though. It is more capable, easier to use, and has built-in functionalities for managing virtual environments.
If you are using pip, enable the secure installs functionality.
Start annotating those types
A type checker can make bigger Python codebases more readable and, as a result, improve collaboration within a team of developers. Quite a few type checkers are available online, so it can take time to choose one. I recommend Facebook’s Pyre type checker, which seems most mature.
As mentioned earlier, check which Python versions your type checker supports before starting a project.
When you want to add type annotations to an existing project, Instagram’s MonkeyType library can prove very useful.
Implement a style guide
Follow the Google style guide or check for alternatives, but avoid creating one yourself.
Even Python needs a build tool
It doesn’t seem like there is much standardization on this front within the Python community. So far, I didn’t require a dedicated build tool that runs tests, linting, type checking, and any other tasks you would like to run.
The most interesting one I found so far is Nox, but I prefer other configuration formats than Python for these kinds of tools.
Use the official Docker Python images
Creating a Docker container for your Python application can be challenging. I recommend using Docker‘s official Python image, as described in this blog post.