Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Formatting With Ruff

Resource mentioned in this lesson: Modern Python Linting With Ruff

00:00 In the last lesson, we talked about the PEP 8 guidelines for the recommended order of your import statements at the top of the file. Now, you could just try and remember these every time, or we could also use a tool called a formatter to help organize these imports for us.

00:15 A formatter is a tool that points out where your code doesn’t conform to PEP 8, and it can automatically organize your import statements as part of that check. There are many different formatters, but one of them, which we are going to use in this lesson, is called ruff.

00:32 If you’re interested in finding out more, we have a whole course about the capabilities of ruff. ruff helps you ensure that your code has consistent formatting and that it conforms to PEP 8 guidelines.

00:46 You can install it as a third-party library using pip, and then use ruff to automatically format your code, including organizing your import statements.

00:57 The main ruff command that we will be covering in this lesson is ruff check. So to check a file for errors, we type ruff check followed by the name of the file.

01:11 ruff doesn’t automatically check for your import statement orders. You can tell it to do that by typing ruff check quote_generator.py, or whatever the name of your file is, space --select, which tells ruff which additional options you’d like it to look at, space capital I, which is how to tell ruff to check the order of your import statements.

01:35 And then optionally, you can also put --fix at the end to apply the fixes for you. Let’s see this in action.

01:44 So over to our code. And what I’m going to do is I’m going to take some of these import statements at the top of our quote_generator.py file.

01:52 I’m just going to select the entire import statements and cut and paste them in a different order.

02:00 So I’m just going to move around these import statements so they no longer conform to PEP 8, because it should be standard library first, then third parties such as rich, and then local imports.

02:13 So I have violated that particular PEP 8 guideline here. I’m going to save the file. And then you’re going to go over to the terminal and type ruff check space the name of your file.

02:29 When you run that command, it’s going to say all checks have passed. And that’s because ruff doesn’t automatically check for the order of import statements.

02:39 To do that, type the same command as before, ruff check space the name of your file. And then you’re going to type --select space capital I.

02:52 And when you run this, you get an error message, because what happens is ruff has now checked for the ordering of the import statements, which is incorrect. And it tells us that that’s the problem at the top.

03:02 There is an import block, which is unsorted or unformatted. And ruff helpfully tells us at the end that it is fixable with the --fix option.

03:12 So let’s apply that fix. I’m going to press up on the keyboard to get back the previous command. So ruff check, name of the file, --select space capital I.

03:24 And if you type --fix at the end, it’s going to apply the fix as well as identify it. So it says found one error and fixed it. So if we go back to our code file, you can see that the order of the import statements at the top is now in the PEP 8 order, starting with the standard library up here, then third-party modules, then local imports.

03:50 So linters can be a useful way to enforce these formatting guidelines without having to remember them every time. In the next lesson, we will talk about another best practice when structuring your Python script, which is using constants.

Become a Member to join the conversation.