My Python Code Looks Ugly and Confusing – Help!
Key Takeaways
This video discusses using automated code analysis tools, specifically a Python code linter called Flake8, to improve code quality and readability. It provides a step-by-step guide on how to install and use Flake8 to detect and fix issues in Python code.
Full Transcript
hey everyone it's dan Bader here and a common complaint that I see from new Python developers or those Python developers who want to take their skills to the next level boils down to saying my Python code looks ugly and it's confusing how can I fix it and there is a couple of ways you can move past that barrier and get better at coding Python and I want to show you one that I've seen work very effectively over and over again and it is using a Python code linter and there's exactly one that it would recommend very much called flake eight because it's a combination of a static analysis tool that you know is going to call you out on some just plain bugs in your Python code and also a Python code style checker which is going to call you out on formatting inconsistencies and just things that are ugly or confusing about your code so I've got a little example here and we can look at this and say okay you know it looks kind of decent like what's run it um creates two random numbers multiplies them seems to work awesome let's ship it but when we actually you know try and read this file look at this it just looks hilariously ugly it is really inconsistent like we're you know doing these things we're importing more than one module per line then the formatting is not consistent we're using different indentation levels we're actually creating this variable here that we're not using there's all these random spaces that shouldn't be here it just kind of seems just really ugly and not very organized now the good news is that there's actually a tool that can help you with these problems and it's called flake eight so let's go ahead and install it right now using pip we're just going to go pip install flake eight run this and once it's done we have a new command available called flake eight and we're just going to run this on our example python file and just like that it's detecting a whole bunch like a whole list of things that are just weird or odd or plain wrong about our code and if you've got your terminal set up correctly and I've got another video that shows you how to do that you can actually click these recommendations and jump directly to that line in your editor so here in the first line flake eight is complaining that we're importing cysts but we're not using it so we want to get rid of Sis here right and then it also complaints about the fact in the same line that we're using multiple imports on one line so again this should be split up and it's finding all these other problems like we're not spacing our functions apart we're using inconsistent indentation and all of these things now this is pretty usable by itself right you might as well run this tool like go and run this tool right now in any Python program you've ever written and you're going to learn tons and I promise it's going to make you a better Python programmer now the other thing you can do is actually integrate flake eight directly with your editor I personally I really like sublime text so I've got this set up here where every time I save a file it runs flake eight and it gives me feedback on my code and I think this is really the secret to moving past that stage where we're you're writing in consistent code that is not formatted correctly and you catch yourself making all these tiny little bugs in your in your day to day work right this can really help you with that because it makes it extremely easy to avoid these mistakes right we can just get rid off these unused imports and then here we want two spaces according to pepp eight here for example you can see that the indentation isn't right now if I run flake eight again I'll actually see that we're creating this variable this local variable that we're not using so let's get rid of it um add some more spaces here all right we've got some inconsistent formatting here as well so let's fix that again same thing here and that's probably complaining because we have too many extra spaces in here and all of a sudden this whole thing just became a lot easier to read and it still works so if you're interested in getting your sublime text set up in a way where it gives you that feedback on your code I wrote a step-by-step guide book that you can find at sublime text Python comm but that shameless plug aside I think for every Python program are watching this right now go and install flake eight this is the tool that I would recommend there's a couple more like pile inter and but flake eight will get you started it is very easy to use it is low and false positive so you will most of the time only call out things are actually just plain wrong or ugly in in the Python coded checks so I've personally seen great results from using flake eight and just getting that constant automatic feedback I've seen great results and co-workers and pretty much anyone I ever talked to and recommended this tool to so go and check it out I absolutely love flake eight and I think it's going to make you a better Python programmer in the shortest amount of time possible all right thanks for listening and good with setting everything up
Original Description
LEARN MORE: https://dbader.org/blog/python-code-linting
Automated code analysis tools can help you become a better Python developer quickly.
In this video I'll explain what a code linter (like Pyflakes, Flake8, or Pycodestyle / Pep8) can do for you.
Next I'll show you a few simple steps you can use immediately to run a code linter on your own Python programs.
I'll also show you how to integrate linter feedback with your code editor (I'm using Sublime Text 3 in the video). This gives you immediate feedback on your Python code as you type it.
I've seen great results from this technique. I believe it's one of the quickest ways to improve your Python skills. Give it a try and let me know how it went :)
* In-depth guide for setting up Sublime Text (with integrated code linting and auto-completion for Python): https://SublimeTextPython.com
* Flake8: https://pypi.python.org/pypi/flake8
* Pyflakes: https://pypi.python.org/pypi/pyflakes
* Pycodestyle (former pep8): https://pypi.python.org/pypi/pycodestyle
* Pylint: https://pypi.python.org/pypi/pylint
* * *
Shameless plug: If you want to get a Sublime Text setup just like the one in the video then check out my Sublime Text Setup Guide for Python Developers: https://SublimeTextPython.com 😊
► Weekly Tips for Python Developers: https://dbader.org/newsletter
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Real Python · Real Python · 26 of 60
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
▶
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 AI Lessons
⚡
⚡
⚡
⚡
Career choice with the advent of AI - pure Computer Science or learn software with a background of core engineering area
Dev.to AI
The AI Hype Cycle: Calm Before the Next Breakthrough?
Medium · Programming
AI won’t replace scientists. It will make the current model of science obsolete
Medium · Data Science
The End of Knowledge: Why Artificial Intelligence Is Changing Not Only What We Know, but What It…
Medium · AI
🎓
Tutor Explanation
DeepCamp AI