# Code linting¶

As well heling you write the code in the first place, an IDE will usually also try to help spot potential errors in your code.

Edit first.py to have the following contents:

first.py
a = 7

print(b)


You may notice yourself that the code here has an error in it. We're creating a variable called a and then using a variable called b. In a short snippet like this it's relatively easy to see it yourself but in a large program, it would be very difficult so having the IDE help out is very useful.

You'll see a red underline beneath the b. If you hover your mouse cursor over it you'll see a popup with a description of the error:

In VS Code, due to the way that it runs the linting, you may have to save the file before the error shows up.

## Fixing errors¶

The IDE is telling us that there is a problem but sometimes it will be able to help us fix the problem too. You should not completely rely on the IDE to fix issues for you but it can certainly be of help.

In PyCharm, the popup that shows when you hover over the error will suggest a few fixes. You'll probably see "Import this name" as one as it's guessing that maybe b is referring to a module we forgot to import. In this case, that is not the correct fix.

Move the mouse and click on "More actions..." (or press Alt+Enter or ⌥+Enter on macOS) and you will see a list of possible fixes that PyCharm thinks could help.

In this case we want to choose "Rename reference" so click that and you will see a list of options for renaming the used variable:

Select a by double-clicking it or selecting it with the keyboard and pressing enter.

To change the name of the used variable in VS Code, you can simply delete the name and type the correct one, a.

## Fixing imports¶

As another example, change the file first.py to:

first.py
a = 7

math.ceil(a)


This will give you an error on line 3 since we are using the math module without having imported it.

Hover over the error with the mouse and you should see a popup like:

You'll see that the default suggested action here is Import 'math' so click the blue words or press Alt+Shift+Enter.

This will automatically add import math to the top of the file.

Manually add import math to the top of the file.

You script should now look like the following without any error mentioned:

first.py
import math

a = 7

math.ceil(a)


## Style fixes¶

As well as things that are broken in your code and will stop it from running correctly, most IDEs can also check for style suggestions so that you can format your code correctly and accoring to commonly-agreed community standards.

Edit first.py to look like:

first.py
a = 7

print( a )


In the IDE you will likely see either red or orange underlining around the a in the print call.

PyCharm has multiple categories of problem: Error (which we've seen already), Warning, Weak Warning (which we see here), Typo and others.

In the same way as we did previously, hover over the orange underlining just after the (:

Click the cog in the bottom-left corner of the screen

and select Settings.

In the search box, type pycodestyle enabled and click the checkbox that shows up next to "Whether to lint Python files using pycodestyle". It will pop up a box in the bottom-right saying "Linter pycodestyle is not installed". Select Install.

Now, you should see a red underline beneath the ( a:

If not, then resave the file.

This is telling us that the space between the ( and a is against the recommendations in PEP 8. This guide is not absolute law and with all style-guides you should discuss it with your collaborators as to what style you follow.

PyCharm does not provide a way of fixing just one single style issue, the only automated option you have is to select Reformat file which will update the whole file to be compliant with its recommended style.

To automatically reformat the file to follow the standard, right click in the file and select Format document. It will pop up a sindow in the bottom-right saying "Formatter autopep8 is not installed. Install?" Either press Yes or Use black. I like using black and it is becoming more standard.

After the install has finished, you may have to re-run Format document.

Alternatively, fix the warnings manually one at a time:

1. Delete the space between ( and a
2. Delete the space between a and )
3. Add a new line after the ) at the end of the file

### Exercise¶

Edit first.py to look like:

python_path = sys.path

print( python_path )


Use the linter and any automated tools to correct and clean up the file so that it no longer has any errors or warnings.