Create efficient Python backend projects: you can’t miss these tools

Although many people know that I have been using Java, what is less known is that for the past few years, I have also been quietly developing backend API server projects in Python. Along the way, I explored and discovered several extremely useful tools. Not only do they optimize my development process, but they also play an important role in teamwork in particular. In this article, I will share these tools and explain why they have become an integral part of my Python projects.

1. pip-compiler

We usually install Python packages with a simple pip install package_name command and list these packages in the “requirements.txt” file within the project folder. However, my team members often express confusion about this file. They report: “This file lists a lot of content, but I don’t understand what packages we installed.”

The continuous expansion of project scale has brought complexity to dependency management. It is a big challenge for us to clearly identify the dependencies between different libraries. In this context, it is particularly important to properly manage these dependencies.

When facing many dependency management challenges, I find the “pip-compilers” library extremely useful. “pip-compilers” is a tool that is unique in that it allows developers to subdivide and manage individual packages through different files. In this way, whether in development or production environment, we can clearly know the purpose of each package and understand their origin. This approach greatly simplifies the management of project dependencies, making the entire process both efficient and reliable.

Below are simple instructions for use.

  • Use the following command to install pip-compilers in the Python environment.

PythonCopy code

pip install pip-tools

  • Create the original requirements.in file.
fastapi
boto3
beanie
cryptography
motor
dependency-injector
gunicorn
requests

  • Compile the requirements.in file into requirements.txt.
pip-compile --output-file requirements-dev.txt [requirements.in](http://requirements.in)

  • We can then see the following document and use that file to install the Python package.
#
# This file is automatically generated by pip-compile, and the Python version used is 3.8
# Generate with the following command:
#
# pip-compile --output-file=requirements-dev.txt requirements.in
#
anyio==3.6.2
    # dependency via starlette
beanie==1.19.1
    # Pass -r requirements.in dependencies
boto3==1.26.141
    # Pass -r requirements.in dependencies
botocore==1.29.141
    # pass
    #boto3
    #s3transfer

...

# The following packages are considered unsafe in the requirements file:
#setuptools

The functionality of pip-compilers is included in the pip-tools library. Therefore, we can use pip-compilers by installing the pip-tools package. If you need more detailed instructions, please refer to the documentation in the repository.

Pip-tools: pypi.org/project/pip…

2. pre-commit

When writing code, it is crucial to maintain consistency and convention, just like the grammatical rules we follow when writing articles. In this process, pre-commit is like a strict teacher, ensuring that every line of code we submit meets the prescribed standards. It does not allow any code that does not comply with the rules to pass, just like the teacher requires that the format of each assignment be consistent before correcting the assignment. The benefits of doing this are obvious: it not only improves code quality, but also avoids post-maintenance costs due to formatting confusion or minor errors.

For example, the consistency of code format, just like the layout of articles, is extremely important for team collaboration. A small indentation error or extra white space may seem innocuous, but in the long run, it will cause a lot of trouble to the project like a cumulative typo disease.

Using the pre-commit tool is like installing a protective door on the code base. Any code that does not comply with the preset rules cannot pass through this door. In this way, we can ensure that only well-crafted and compliant code is included in the code base, just like only articles that have been proofread and correct are published.

Next, let’s install pre-commit. Follow the steps below and you will find that introducing such a tool is actually a piece of cake.

  • Install
# Use pip
pip install pre-commit

# Add the following to your requirements.txt file
pre-commit

  • Add the “.pre-commit-config.yaml” file to your project root directory.
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.4.0
    hooks:
    - id: trailing-whitespace
    - id: end-of-file-fixer
        exclude: voice_biz.dmm
    - id: check-yaml
    - id: check-added-large-files
- repo: https://github.com/pycqa/flake8
    rev: 6.0.0
    hooks:
    - id: flake8
        exclude: src/tests
        additional_dependencies: [flake8-docstrings]

  • Install git hook script
pre-commit install

In this way we have completed the preparations for using pre-commit.

Of course you can test it by submitting, but there is an easy way to test it.

pre-commit run --all-files

Using pre-commit is like installing a smart assistant in our code base. When you are ready to submit your code, it will automatically check whether all your packages are used. It’s like having an assistant remind you to bring your keys and mobile phone before you go out to prevent you from forgetting them. Such pre-checking significantly improves the cleanliness and efficiency of the code base.

But the cleverness of pre-commit doesn’t end there. Its design allows us to add rules according to our needs. For example, you want every new function or class to have test code to ensure the robustness of the code. Then, we can write a rule for this goal and let pre-commit check it for you before you submit the code. It’s like adding an extra layer of quality assurance to our programming practices.

To learn more about pre-commit, you can visit its official website: pre-commit.com

In short, using pre-commit, we can not only keep the code base clean and tidy, but also ensure that coding standards are followed, so that the code quality of the entire team is improved.


———————————END——————- ——–

Digression

Thank you for watching until the end, I have prepared some benefits for everyone!

Interested friends will receive a complete set of Python learning materials, including interview questions, resume information, etc. See below for details.


CSDN gift package:The most complete “Python learning materials” on the entire network are given away for free! (Safe link, click with confidence)

1. Python learning routes in all directions

The technical points in all directions of Python have been compiled to form a summary of knowledge points in various fields. Its usefulness is that you can find corresponding learning resources according to the following knowledge points to ensure that you learn more comprehensively.

img
img

2. Python essential development tools

The tools have been organized for you, and you can get started directly after installation! img

3. Latest Python study notes

When I learn a certain basic and have my own understanding ability, I will read some books or handwritten notes compiled by my seniors. These notes record their understanding of some technical points in detail. These understandings are relatively unique and can be learned. to a different way of thinking.

img

4. Python video collection

Watch a comprehensive zero-based learning video. Watching videos is the fastest and most effective way to learn. It is easy to get started by following the teacher’s ideas in the video, from basic to in-depth.

img

5. Practical cases

What you learn on paper is ultimately shallow. You must learn to type along with the video and practice it in order to apply what you have learned into practice. At this time, you can learn from some practical cases.

img

6. Interview Guide

Resume template

CSDN gift package:The most complete “Python learning materials” on the entire network are given away for free! (Safe link, click with confidence)

If there is any infringement, please contact us for deletion.