Run Ruff on Your Python Code: ruff check and ruff rule Explained
Skills:
AI-Assisted Code Review80%
Key Takeaways
Explains how to use Ruff to check Python code for linting errors
Full Transcript
Now that you've installed rough, you're ready to start using it to check your code for linting errors. And you will do that with a subcomand rough check. Not necessarily often, but it might happen that you will link your code. And rough reports a linting error that you've never seen before and that you don't really understand. And so you will also learn how to use the subcommand rough rule to get more information about that error. And you will see everything in action in just a second. You just need some code to work with, right? So, go ahead and download the file one ring. py from the course materials. You should be able to download this file and move it to a directory where you're comfortable working in. Once you do that, let's just take a quick look at what the file looks like. So, the file one ring py should have roughly 20 lines of Python code. It should have a global variable called characters which is very long, doesn't fit on the screen. It has a couple of functions and then a couple of if statements at the bottom of the file. And this is the code you're going to be working with. So the first thing you'll do is open your terminal and make sure you've navigated to the directory where the file one ring. py is right now. If you use the command ls, you can see that you are in the correct directory. And once you're there, you can run the subcomand rough check to check if there are any errors in your code. And once you run rough check, you get two different errors on the screen. Now, when rough presents an error, the error will start with an error code, which is typically one or a couple of letters followed by some numbers. and then one line, a short sentence describing the error. Ruff will also show you the context, the file and the line where the error is found and also the surrounding code. And sometimes it will also show you some help text giving hints or suggestions as to what you could do to fix the error. Now, the thing is sometimes these errors aren't very easy to understand in the first place. And when that's the case, you can use the subcomand ref rule to get more information about that error. For example, if you look at the second error, it has the error code F821. So what you can do is you can type the subcomand rough rule and then F821. And if you press enter, you get some markdown text describing the error that you got, what the error does, how to fix it, or some examples of how you can fix the error, options that might be relevant, references in case you want to learn more. So, rough will give you a lot of information about different errors and this is very helpful if you get an error that you've never seen or an error that you seldom see and that you don't really remember what it means. Now, when you check your code, you run the subcomand rough check. And what this subcomand does by default is go through all files in the current directory, traverse the directory recursively and check every single file. But you can also provide a subfolder or a specific file to check. So if you ran rough check one ring py, you would check explicitly only the file that you specified. So that's very helpful. And another thing that might be of interest is that the subcomand rough rule accepts an option d- all which lets you take a look at all errors that rough could possibly report. You can't see it on the screen because it was a lot of text and it scrolled past really really fast. But this is a lot of output. It's not convenient to scroll up to the top to find the beginning. You might not even have enough history length to be able to scroll to the beginning. So one thing you might want to do is run rough rule d-pole and then pipe. So the vertical bar character and pipe that output to the command less which will let you page through the output of all of the rules and therefore making it easier to take a look at all of the rules if if you wanted to just read about them from start to finish. So you can use the up and down arrows to go up and down the output. And when you want to quit, just press Q. Now, this is just a curiosity. It's unlikely that you'll want to read the whole document from start to finish, but at least you know the information is there. If the internet goes down, you can still access the documentation about each rule that Ruff can check for. And now that you know how to check for linting issues, it's time to learn how to fix them with Rough. And that's what you'll do in the next lesson.
Original Description
Download your free Python Cheat Sheet here: https://realpython.com/cheatsheet
Free Python Skill Test with instant level + learning plan: https://realpython.com/skill-test
Want to learn faster? Become a Python Expert with unlimited access to 5,000+ tutorials, videos, and exercises: https://realpython.com/start
This lesson focuses on checking your code for linting errors and understanding Ruff’s output so you know exactly what each rule means.
• Use the sample file one_ring.py from the course materials
• Run ruff check on the current directory and on a single file
• See how Ruff reports linting errors with codes, messages, file names, and line numbers
• Use ruff rule with a specific error code to get detailed, Markdown-style documentation
• List all available Ruff rules with ruff rule --all
• Pipe the output of ruff rule --all into less so you can scroll through all rules
• Access Ruff’s documentation even when the internet is down
Watch the full course:
https://realpython.com/courses/modern-python-linting-ruff/
🐍 Become a Python expert with real-world tutorials, on-demand courses, interactive quizzes, and 24/7 access to a community of experts at https://realpython.com
▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰
🐍 Start Here → https://realpython.com/start
🗺️ Guided Learning Paths → https://realpython.com/learning-paths
🎧 Real Python Podcast → https://realpython.com/podcast
📚 Python Books → https://realpython.com/books
📖 Python Reference → https://realpython.com/ref
🧑💻 Quizzes & Exercises → https://realpython.com/quizzes
🎓 Live Courses: https://realpython.com/live
⭐️ Reviews & Learner Stories: https://realpython.com/learner-stories
▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Real Python · Real Python · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
A better Python REPL – bpython vs python interpreter
Real Python
Introducing large-type.com – A Utility Website
Real Python
Reading Hacker News Without Wasting Tons of Time
Real Python
Forward References and Python 3 Type Hints
Real Python
Using Sublime Text as your Git Editor
Real Python
Python Code Linting and Auto-Complete for Sublime Text
Real Python
Make your Python Code More Readable with Custom Exceptions
Real Python
Write Better Tests with Sublime Text's Split Layout Feature
Real Python
How to Use Sublime Text from the Command Line
Real Python
Rename Variables with Multiple Selection in Sublime Text
Real Python
Sublime Text Settings for Writing PEP 8 Python
Real Python
Write Cleaner Python with Sublime Text's Indent Guides
Real Python
Sublime Text Whitespace Settings for Python Development
Real Python
Function Argument Unpacking in Python
Real Python
Python Code Review: Debugging and Refactoring "Conway's Game of Life" + Automated Tests
Real Python
Using "get()" to Return a Default Value from a Python Dict
Real Python
A Python Shorthand for Swapping Two Variables
Real Python
Python Code Review: Refactoring a Web Scraper, PEP 8 Style Guide Compliance, requirements.txt
Real Python
Click & Jump to Test Failures from the Command Line (iTerm2)
Real Python
Setting up Sublime Text for Python Developers
Real Python
Sublime Text + Python Guide Overview
Real Python
Python Code Review: Adding Pytest Tests to an Existing Python Web Scraper
Real Python
Type-Checking Python Programs With Type Hints and mypy
Real Python
A Shorthand for Merging Dictionaries in Python 3.5+
Real Python
Python Code Review Flask Web Security Tutorial + Virtualenvs, requirements.txt
Real Python
My Python Code Looks Ugly and Confusing – Help!
Real Python
Setting Up a Programmer Portfolio/Developer Blog – How To Get Started
Real Python
Do I Need a GitHub/GitLab/Bitbucket Profile as a Developer?
Real Python
Programmer Portfolio – Example and Walkthrough
Real Python
How to Get Your 1st Speaking Gig at a Tech Conference
Real Python
How to Build Your Public Speaking Skills as a Developer
Real Python
The Object-oriented Version of "Spaghetti Code" is "Lasagna Code" ?!
Real Python
Setting up Sublime Text for Python Developers – Lesson #1
Real Python
Cool New Features in Python 3.6
Real Python
"is" vs "==" in Python – What's the Difference? (And When to Use Each)
Real Python
Emulating switch/case Statements in Python with Dictionaries
Real Python
Python Function Argument Unpacking Tutorial (* and ** Operators)
Real Python
What Code Should I Put On My GitHub/GitLab/BitBucket Profile?
Real Python
A Crazy Python Dictionary Expression ?!
Real Python
String Conversion in Python: When to Use __repr__ vs __str__
Real Python
Method Types in Python OOP: @classmethod, @staticmethod, and Instance Methods
Real Python
Optional Arguments in Python With *args and **kwargs
Real Python
Python Context Managers and the "with" Statement (__enter__ & __exit__)
Real Python
Installing Python Packages with pip and virtualenv / venv
Real Python
"For Each" Loops in Python with enumerate() and range()
Real Python
Python Code Review: LibreOffice Automation and the Python Standard Library
Real Python
Managing Python Dependencies With Pip and Virtual Environments – Lesson #1
Real Python
Python Tutorial: List Comprehensions Step-By-Step
Real Python
Leveraging Python's Implicit "return None" Statements
Real Python
What's the meaning of underscores (_ & __) in Python variable names?
Real Python
Python Data Structures: Sets, Frozensets, and Multisets (Bags)
Real Python
Writing automated tests for Python command-line apps and scripts
Real Python
How to find great Python packages on PyPI, the Python Package Repository
Real Python
Immutable vs Mutable Objects in Python
Real Python
PyPI vs Warehouse, the Next-Generation Python Package Repository
Real Python
pep8.org — The Prettiest Way to View the PEP 8 Python Style Guide
Real Python
My Experience at PyCon 2017 in Portland
Real Python
Pylint Tutorial – How to Write Clean Python
Real Python
"Reverse a List in Python" Tutorial: Three Methods & How-to Demos
Real Python
Python Refactoring: "while True" Infinite Loops & The "input" Function
Real Python
More on: AI-Assisted Code Review
View skill →Related Reads
📰
📰
📰
📰
Clipdrop vs Runway vs Kling: Which is Best for Drama in 2026?
Dev.to AI
The Ultimate Guide to Crafting a Stellar Resume with AI Technology
Dev.to AI
Observability Design for the AI Era — Application / Infrastructure / CI / LLM, Each in Its Own Shape (Part 1)
Dev.to · Ryosuke Tsuji
The AI Resource I Could Never Find — So I Decided to Build It
Medium · AI
🎓
Tutor Explanation
DeepCamp AI