Run Ruff on Your Python Code: ruff check and ruff rule Explained

Real Python · Beginner ·🛠️ AI Tools & Apps ·6mo ago

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 A better Python REPL – bpython vs python interpreter
A better Python REPL – bpython vs python interpreter
Real Python
2 Introducing large-type.com – A Utility Website
Introducing large-type.com – A Utility Website
Real Python
3 Reading Hacker News Without Wasting Tons of Time
Reading Hacker News Without Wasting Tons of Time
Real Python
4 Forward References and Python 3 Type Hints
Forward References and Python 3 Type Hints
Real Python
5 Using Sublime Text as your Git Editor
Using Sublime Text as your Git Editor
Real Python
6 Python Code Linting and Auto-Complete for Sublime Text
Python Code Linting and Auto-Complete for Sublime Text
Real Python
7 Make your Python Code More Readable with Custom Exceptions
Make your Python Code More Readable with Custom Exceptions
Real Python
8 Write Better Tests with Sublime Text's Split Layout Feature
Write Better Tests with Sublime Text's Split Layout Feature
Real Python
9 How to Use Sublime Text from the Command Line
How to Use Sublime Text from the Command Line
Real Python
10 Rename Variables with Multiple Selection in Sublime Text
Rename Variables with Multiple Selection in Sublime Text
Real Python
11 Sublime Text Settings for Writing PEP 8 Python
Sublime Text Settings for Writing PEP 8 Python
Real Python
12 Write Cleaner Python with Sublime Text's Indent Guides
Write Cleaner Python with Sublime Text's Indent Guides
Real Python
13 Sublime Text Whitespace Settings for Python Development
Sublime Text Whitespace Settings for Python Development
Real Python
14 Function Argument Unpacking in Python
Function Argument Unpacking in Python
Real Python
15 Python Code Review: Debugging and Refactoring "Conway's Game of Life" +  Automated Tests
Python Code Review: Debugging and Refactoring "Conway's Game of Life" + Automated Tests
Real Python
16 Using "get()" to Return a Default Value from a Python Dict
Using "get()" to Return a Default Value from a Python Dict
Real Python
17 A Python Shorthand for Swapping Two Variables
A Python Shorthand for Swapping Two Variables
Real Python
18 Python Code Review: Refactoring a Web Scraper, PEP 8 Style Guide Compliance, requirements.txt
Python Code Review: Refactoring a Web Scraper, PEP 8 Style Guide Compliance, requirements.txt
Real Python
19 Click & Jump to Test Failures from the Command Line (iTerm2)
Click & Jump to Test Failures from the Command Line (iTerm2)
Real Python
20 Setting up Sublime Text for Python Developers
Setting up Sublime Text for Python Developers
Real Python
21 Sublime Text + Python Guide Overview
Sublime Text + Python Guide Overview
Real Python
22 Python Code Review: Adding Pytest Tests to an Existing Python Web Scraper
Python Code Review: Adding Pytest Tests to an Existing Python Web Scraper
Real Python
23 Type-Checking Python Programs With Type Hints and mypy
Type-Checking Python Programs With Type Hints and mypy
Real Python
24 A Shorthand for Merging Dictionaries in Python 3.5+
A Shorthand for Merging Dictionaries in Python 3.5+
Real Python
25 Python Code Review Flask Web Security Tutorial + Virtualenvs, requirements.txt
Python Code Review Flask Web Security Tutorial + Virtualenvs, requirements.txt
Real Python
26 My Python Code Looks Ugly and Confusing – Help!
My Python Code Looks Ugly and Confusing – Help!
Real Python
27 Setting Up a Programmer Portfolio/Developer Blog – How To Get Started
Setting Up a Programmer Portfolio/Developer Blog – How To Get Started
Real Python
28 Do I Need a GitHub/GitLab/Bitbucket Profile as a Developer?
Do I Need a GitHub/GitLab/Bitbucket Profile as a Developer?
Real Python
29 Programmer Portfolio – Example and Walkthrough
Programmer Portfolio – Example and Walkthrough
Real Python
30 How to Get Your 1st Speaking Gig at a Tech Conference
How to Get Your 1st Speaking Gig at a Tech Conference
Real Python
31 How to Build Your Public Speaking Skills as a Developer
How to Build Your Public Speaking Skills as a Developer
Real Python
32 The Object-oriented Version of "Spaghetti Code" is "Lasagna Code" ?!
The Object-oriented Version of "Spaghetti Code" is "Lasagna Code" ?!
Real Python
33 Setting up Sublime Text for Python Developers – Lesson #1
Setting up Sublime Text for Python Developers – Lesson #1
Real Python
34 Cool New Features in Python 3.6
Cool New Features in Python 3.6
Real Python
35 "is" vs "==" in Python – What's the Difference? (And When to Use Each)
"is" vs "==" in Python – What's the Difference? (And When to Use Each)
Real Python
36 Emulating switch/case Statements in Python with Dictionaries
Emulating switch/case Statements in Python with Dictionaries
Real Python
37 Python Function Argument Unpacking Tutorial (* and ** Operators)
Python Function Argument Unpacking Tutorial (* and ** Operators)
Real Python
38 What Code Should I Put On My GitHub/GitLab/BitBucket Profile?
What Code Should I Put On My GitHub/GitLab/BitBucket Profile?
Real Python
39 A Crazy Python Dictionary Expression ?!
A Crazy Python Dictionary Expression ?!
Real Python
40 String Conversion in Python: When to Use __repr__ vs __str__
String Conversion in Python: When to Use __repr__ vs __str__
Real Python
41 Method Types in Python OOP: @classmethod, @staticmethod, and Instance Methods
Method Types in Python OOP: @classmethod, @staticmethod, and Instance Methods
Real Python
42 Optional Arguments in Python With *args and **kwargs
Optional Arguments in Python With *args and **kwargs
Real Python
43 Python Context Managers and the "with" Statement (__enter__ & __exit__)
Python Context Managers and the "with" Statement (__enter__ & __exit__)
Real Python
44 Installing Python Packages with pip and virtualenv / venv
Installing Python Packages with pip and virtualenv / venv
Real Python
45 "For Each" Loops in Python with enumerate() and range()
"For Each" Loops in Python with enumerate() and range()
Real Python
46 Python Code Review: LibreOffice Automation and the Python Standard Library
Python Code Review: LibreOffice Automation and the Python Standard Library
Real Python
47 Managing Python Dependencies With Pip and Virtual Environments – Lesson #1
Managing Python Dependencies With Pip and Virtual Environments – Lesson #1
Real Python
48 Python Tutorial: List Comprehensions Step-By-Step
Python Tutorial: List Comprehensions Step-By-Step
Real Python
49 Leveraging Python's Implicit "return None" Statements
Leveraging Python's Implicit "return None" Statements
Real Python
50 What's the meaning of underscores (_ & __) in Python variable names?
What's the meaning of underscores (_ & __) in Python variable names?
Real Python
51 Python Data Structures: Sets, Frozensets, and Multisets (Bags)
Python Data Structures: Sets, Frozensets, and Multisets (Bags)
Real Python
52 Writing automated tests for Python command-line apps and scripts
Writing automated tests for Python command-line apps and scripts
Real Python
53 How to find great Python packages on PyPI, the Python Package Repository
How to find great Python packages on PyPI, the Python Package Repository
Real Python
54 Immutable vs Mutable Objects in Python
Immutable vs Mutable Objects in Python
Real Python
55 PyPI vs Warehouse, the Next-Generation Python Package Repository
PyPI vs Warehouse, the Next-Generation Python Package Repository
Real Python
56 pep8.org — The Prettiest Way to View the PEP 8 Python Style Guide
pep8.org — The Prettiest Way to View the PEP 8 Python Style Guide
Real Python
57 My Experience at PyCon 2017 in Portland
My Experience at PyCon 2017 in Portland
Real Python
58 Pylint Tutorial – How to Write Clean Python
Pylint Tutorial – How to Write Clean Python
Real Python
59 "Reverse a List in Python" Tutorial: Three Methods & How-to Demos
"Reverse a List in Python" Tutorial: Three Methods & How-to Demos
Real Python
60 Python Refactoring: "while True" Infinite Loops & The "input" Function
Python Refactoring: "while True" Infinite Loops & The "input" Function
Real Python

Related Reads

📰
Clipdrop vs Runway vs Kling: Which is Best for Drama in 2026?
Learn which AI tool is best for drama in 2026 by comparing Clipdrop, Runway, and Kling
Dev.to AI
📰
The Ultimate Guide to Crafting a Stellar Resume with AI Technology
Learn how to craft a stellar resume with AI technology to increase your chances of getting hired
Dev.to AI
📰
Observability Design for the AI Era — Application / Infrastructure / CI / LLM, Each in Its Own Shape (Part 1)
Learn to design observability for AI applications, infrastructure, CI, and LLMs, with unique shapes for each axis.
Dev.to · Ryosuke Tsuji
📰
The AI Resource I Could Never Find — So I Decided to Build It
Learn how to build a resource for practical AI tools and insights for small businesses, and why it matters for entrepreneurs and businesses
Medium · AI
Up next
I Found FREE Versions of EVERY Paid AI Tool (only top 1% Know this)🔥
Damini Tripathi
Watch →