Skip to content

Latest commit

 

History

History
115 lines (73 loc) · 4.31 KB

File metadata and controls

115 lines (73 loc) · 4.31 KB

Tidying Your Python Files with black and isort

Using black

Black is a popular code formatting tool for Python that helps you maintain a consistent and readable code style. It automatically reformats your code according to PEP 8 style guidelines and enforces a consistent code style across your entire project. In this tutorial, we'll walk through the process of installing and using Black to tidy up your Python code.

Installation

To get started, you need to install Black. You can use pip to install it:

pip install black

Usage

After installing Black, you can use it to format your Python code. To format a single file, you can simply run:

black your_file.py

To format an entire directory, you can use the following command:

black your_directory/

Black will automatically scan your Python files, format them, and overwrite the original files. It doesn't prompt for confirmation, so make sure to back up your code if needed.

Customizing Black

Black comes with a default set of formatting rules that align with PEP 8, but you can customize its behavior by using configuration files or command-line options.

Configuration Files

You can create a pyproject.toml file in your project's root directory to specify Black's configuration. Here's an example of a pyproject.toml file:

[tool.black]
line-length = 79
target-version = ['py36']
exclude = '\.venv|_build|tmp/'

In this example, we've set the line length to 79 characters and specified Python 3.6 as the target version. You can also exclude certain directories or files from being formatted by using regular expressions.

Command-Line Options

Black provides several command-line options that allow you to control its behavior. For example, you can set the line length with the --line-length option:

black --line-length 100 your_file.py

You can run black --help to see a list of all available options.

Conclusion

Black is a powerful tool for ensuring consistent code formatting in your Python projects. By following the installation and usage instructions in this tutorial, you can easily integrate Black into your workflow and maintain a clean and readable codebase.

Using isort

isort is another popular Python tool that helps you organize and sort your import statements according to PEP 8 guidelines. It's especially useful for ensuring a consistent and clean import structure in your Python code. In this tutorial, we'll cover the installation and usage of isort.

Installation

You can install isort using pip:

pip install isort

Usage

Using isort is straightforward. You can format a single file or an entire directory. Here's how to format a single file:

isort your_file.py

To format an entire directory, you can run:

isort your_directory/

By default, isort will automatically sort and organize your import statements within the specified file or directory.

Customizing isort

isort provides various configuration options to tailor its behavior to your project's specific needs. You can create an isort.cfg file in your project's root directory to specify your configuration. Here's an example of an isort.cfg file:

[isort]
line_length = 88
multi_line_output = 3
known_third_party = requests
known_first_party = mymodule
default_section = THIRDPARTY

In this example:

  • line_length sets the maximum line length for your imports.
  • multi_line_output specifies how multiple imports should be displayed.
  • known_third_party lists third-party libraries you want to treat as external.
  • known_first_party lists the modules or packages that belong to your project.
  • default_section specifies where to place imports that don't match any other section.

You can customize these options and more in your isort.cfg file. Run isort --help to see a list of available configuration options and their descriptions.

Conclusion

isort is a valuable tool for keeping your Python import statements well-organized and compliant with PEP 8 standards. By following the installation and usage guidelines in this tutorial and customizing it as needed, you can maintain a clean and readable import structure in your Python projects.