Skip to content

Modern Python Development Tools#

2024 iThome Ironman

This is the 5th article in the Django Ninja tutorial series.

In modern software development, tools not only improve development efficiency but also enhance code quality, making project management and maintenance much easier.

This article introduces several key Python development tools and discusses their application in projects, helping readers quickly understand their positioning and value.

If you have visited my blog, you will find that I have dedicated significant effort to promoting these modern development tools, writing detailed tutorial articles for each of them.

Therefore, we will not dive into the operational details of these tools but instead focus on concepts and how they solve pain points in development. I will provide links to relevant tutorial articles for readers who want to learn more.

It is worth mentioning that these modern Python development practices were learned and implemented after I read developer Wei Lee's "Python Table Manners" series. Highly recommended!

GitHub Example Project#

👉 Django-Ninja-Tutorial


The project uses Python 3.12.5, Django 4.2 LTS, and Django Ninja 1.3.0. This is just for your information.

Let's dive into the introduction of each tool.


pyenv: Python Version Management#

pyenv (yes, its 'p' is lowercase, same as pytest) is a powerful tool for managing multiple Python versions. It allows you to use different versions of Python in different projects without conflict.

This is also why there is a .python-version file in the project—it declares the Python version used by the project to pyenv.

pyenv is particularly helpful when dealing with multiple projects using different Python versions, especially when some projects require older Python versions.

With pyenv, you can easily install and switch Python versions, ensuring each project runs in the correct environment.

For detailed pyenv tutorials, you can refer to my previous articles:


Poetry: Virtual Environment and Package Management#

Poetry is currently a very popular Python package management tool, and package management—especially dependency conflicts—has always been a major pain point in Python development.

Poetry primarily resolves dependency conflicts caused by packages requiring different version ranges.

These details are too long to explain here. You can refer to my article: Python Package Manager — Poetry Complete Beginner's Guide, especially the "Glossary" section.

Additionally, since Poetry involves virtual environment management, how to use it with pyenv to select the correct Python version for the virtual environment is also a common question. You can refer to the article: Poetry + pyenv Tutorial: Common Commands and Precautions.


Ruff: Linter and Formatter#

Times are changing. In the past, I used Flake8 + isort + Black—now I use Ruff.

Ruff is an extremely fast Python linter and formatter written in Rust, used to quickly check and correct code layout and formatting issues.

Fast, just fast

Ruff helps developers maintain a consistent code style (primarily PEP 8 compliance). Its performance makes it particularly well suited to large projects.

Additionally, Ruff integrates numerous Flake8 plugins, making it an All-in-One solution. For me, this is another major selling point besides its speed.

For more information about Ruff, you can refer to my articles:


pre-commit: Git Hooks Management Tool#

As the saying goes: "If there's no CI, at least have pre-commit"—which is a saying I made up.

pre-commit (yes, its 'p' is also lowercase) is a Git Hooks management tool written in Python, applicable to projects in various programming languages. It allows users to set up relevant Git Hooks based on their needs.

Using the common "pre-commit hook" as an example, it automatically runs various check and formatting tools (i.e., the hooks you configured) when you run git commit, ensuring the committed code complies with standards.

This not only maintains code quality but also reduces minor issues in code reviews, allowing reviewers to focus on logic and architecture reviews.

In our example project, pre-commit is configured to run Ruff and some basic format checks to ensure consistent code style.

Related article: Python Development: Tutorial on Setting Up Git Hooks with pre-commit


Mypy: Static Type Checking#

Does your Python project have type hints?

As mentioned in the first article of the series, Django Ninja is a great place to start learning type hints.

To add type hints to a project, a static type checker is indispensable. The most common choice is Mypy.

Mypy is a powerful tool that not only checks for type errors but also catches many potential issues before the code runs. In my view, its impact goes beyond types.

Mypy is a must-have tool for large projects and teamwork. However, I want to emphasize that even for small projects, Mypy brings obvious benefits.

The cost and benefits of type hints can be illustrated by the following diagram (re-drawn from Robust Python):

In the long run, using type hints and Mypy brings continuous benefits—in fact, this applies to all tools introduced in this article😎. Although it requires some time to learn and adapt initially, it is worth it.

For more introduction to Mypy and Python type hints, you are welcome to refer to my "Mypy Trilogy" series:

  1. Notes on "Robust Python": How to Effectively Import Type Hints
  2. Python Type Checker: Introduction to Mypy
  3. Guide to Adding Mypy to Django Projects

Conclusion#

pyenv helps us flexibly manage Python versions, Poetry solves package management issues, and tools like Ruff, pre-commit, and Mypy ensure clean and consistent code. These tools complement each other, collectively building a highly efficient development environment.

Using them well is an essential skill for modern developers and a cornerstone of effective team collaboration. They take time to learn, but once you become familiar with them, I believe they will become indispensable parts of your development workflow.