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

Unlock This Lesson

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

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Using Linters for Better Code & Conclusion

Give Feedback

You’ve seen the criteria used by PEP8 to judge high quality code. In this lesson, you’ll learn how to use some tools to make sure your code stays PEP 8 compliant. You’ll learn about linters, debuggers and autoformatters. The lesson covers pycodestyle, flake8 and black.

You’ll see how to install, run, and configure these tools to suit your project’s requirements.

00:00 You’ve made it through the basics of the style considerations PEP 8 is all about. In this last video, you’ll learn how to use some tools to help make sure your code stays PEP 8 compliant even if you don’t remember. Before that, let’s talk briefly about when you don’t want to follow PEP 8. The answer is basically never. Sometimes, though, you need to take extra considerations.

00:23 Some of these are: if compliance would break compatibility with other software, surrounding code is not PEP 8 compliant, or if your code needs to remain compatible with older versions of Python that may not behave the same way. With this in mind, you should always try to start projects with PEP 8 compliance as this will make sticking to it much easier.

00:43 So before you go to start that next project, let’s discuss some tools to help you follow the guidelines. We’re going to cover linters and autoformatters. Linters will help you debug issues by alerting you to them before you run your code and provide suggestions on how to fix them.

01:00 You can usually find an extension for your text editor that will flag problems as you write code, but we’ll cover two command line versions, pycodestyle and flake8. To get pycodestyle, go to your terminal

01:13 and just type in pip install pycodestyle.

01:20 All right, so pycodestyle is a linter that will check your Python code against the style conventions of PEP 8. So if you look here, I’ve got a couple issues—I don’t have a space after this hash sign for this comment, I don’t have any whitespace here, the whitespace is all mixed up here, this is using an == instead of is.

01:40 There’s quite a few PEP 8 issues. To use pycodestyle, you can type in pycodestyle and then the name of your script. And once you run that, you’ll get an output that will contain all of your PEP 8 issues.

01:56 So here, I’ve got a block comment should start with '# ', which it doesn’t. I’m missing whitespace after some commas. I have a comparison to None that should be if the condition is None. Some more whitespace, no whitespace around an operator—and you get a pretty good idea of where you need to fix things because you’ve got your line number and your character number of where the problem is. So if we go back to the script—you’ll note, this is the start of the script here. So define a my_list, but then this arg is not defined.

02:29 So sometimes you want to include a debugger with your linter. A debugger is just going to try to catch those errors that you’d run into when you actually run the script. One debugger that you can use is called flake8.

02:40 So just pycodestyle, you can do pip install flake8.

02:49 And what this does is combine pycodestyle with pyflakes, which is a debugger. Run it just in the same way that you did pycodestyle.

02:58 I can just say flake8 pep_talk.py.

03:03 And if I open this up, you can see all the same PEP 8 issues as before, but now also this undefined name 'arg', which as you can see, this has not been defined yet.

03:17 So flake8 can be a nice combination of a debugger and a PEP 8 linter. It’ll give you a little more bang for the buck when you go to run it.

03:26 Let’s say you’ve got a script that’s, you know, a couple hundred lines long, maybe a thousand lines long. You run your linter and you get a ton of issues that you don’t have the time to fix. In this case, you can try out an autoformatter.

03:41 These are programs that will actually refactor your code to meet PEP 8 requirements. Let me close this terminal out.

03:49 If you go to open up a new terminal, one autoformatter you can try is called black. black is a good autoformatter that follows most of PEP 8.

04:03 You should note that you need to have at least Python 3.6 installed to use black. I’m actually going to make this terminal a little bit smaller so that you can see the entire script here. And if you watch, when I run black with the script and hit Enter, like magic—I’ve had whitespace appear here, there’s whitespace here, there’s whitespace after these commas, and everything looks much better.

04:32 You can note that it didn’t do anything to fix this argument problem here, where this value is not defined yet. And if you look at the output, it gives you this nice little message that it’s all done and tells you that it reformatted 1 file.

04:45 Another thing to note about black is that it doesn’t exactly follow PEP 8, and one of the biggest deviations is that its default line length is 88 characters, as opposed to PEP 8’s 79-character limit.

04:59 You can actually fix this with a command line argument. Before running black and then your script, you can actually put in --line-length=79 and then your script. Run it like that, and it’ll do the same thing.

05:17 You can see that because I didn’t have any lines that were that long, it just says that 1 file left unchanged. And there you have it! So now not only do you know some of the criteria that PEP 8 uses to judge high-quality code, you have a couple tools in your arsenal to make sure that you’re following the rules. If you’re interested in learning more about PEP 8, you can go to pep8.org to see a formatted version of the enhancement proposal. Congrats on getting through this! PEP 8 definitely isn’t one of the most exciting Python topics, but it’s critical if you want to collaborate with others and continue improving with Python.

05:55 You’ll never go wrong by following the PEP 8 criteria, so try to do so as much as possible. Thanks for watching.

arnautovdmitry2015 on April 2, 2019

Thank you, Joe, for this nice course. It was very helpful.

Joe Tatusko RP Team on April 2, 2019

You are very welcome! Just a couple tweaks to how you write code can make a huge difference

Abby Jones on June 25, 2019

This was fantastic, and normally something I wouldn’t have looked at. This was extremely valuable.

victorariasvanegas on Dec. 1, 2019

Thanks really good course.

Pakorn on Dec. 11, 2019

great course, Thanks.

km on Jan. 3, 2020

Another great piece of content

swapnilc17 on Jan. 9, 2020

Nice course, Thank you.

gmodelgado on Jan. 28, 2020

Cool Course, Thanks!!

xiliang on April 5, 2020

Very helpful. I have coding in Python as a data scientist for 2 years and I just realized I have never followed the PEP8 guideline. Shame on me! Keep up the good work.

Ghani on Oct. 26, 2020

Very informative course; thank you so much!

Become a Member to join the conversation.