-2

I'm trying to understand the error below from Flake8:

no newline at end of fileFlake8(W292)

This is my code below:

if __name__ == "__main__":
    app.run(
        host=os.environ.get("IP", "0.0.0.0"),
        port=int(os.environ.get("PORT", "5000")),
        debug=False)

And, the error is pointing to the last line below:

debug=False

Can someone help explain to me why I'm getting this invalid error and offer a solution to solve it. Thanks

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Ryu
  • 1
  • 1
  • 6
  • 2
    It means exactly what it says. There is no recognizable newline at the end of the file. The last character is the `)` ... or maybe a TAB, or SPACE, or a line terminator for a different platform. Solution: open the file in an editor, add a newline at the end, save and close the file. – Stephen C Apr 23 '22 at 02:57
  • I've tried that, I had a new line after it and I get "blank line contains whitespace" error – Ryu Apr 23 '22 at 03:00
  • So get rid of the whitespace!! – Stephen C Apr 23 '22 at 03:01
  • @Ryu That's a different error. Ultimately what you should do is set up your editor to do all of this for you: add a trailing newline if there isn't one, and strip trailing whitespace. For example for Atom, there's a package called [whitespace](https://atom.io/packages/whitespace) that'll do that. – wjandrea Apr 23 '22 at 03:03
  • Solved. Thanks for your help. I wasn't using my common sense – Ryu Apr 23 '22 at 03:03
  • Do you know if there's one for VS Code – Ryu Apr 23 '22 at 03:04
  • @Ryu Yeah, looks like there are settings built-in. See these questions: [Insert New Line at the End of Files](/q/44704968/4518341) and [Remove trailing spaces automatically or with a shortcut](/q/30884131/4518341) – wjandrea Apr 23 '22 at 03:09

2 Answers2

3

It means exactly what it says. There is no recognizable newline at the end of the file. The last character is the ) ... or maybe a TAB, or SPACE, or a line terminator for a different platform.

Solution: open the file in an editor, add a newline at the end, save and close the file.


I've tried that, I had a new line after it and I get "blank line contains whitespace" error.

So what you had was a line consisting of white space (SPACE or TAB characters) followed by a newline. Use your editor to get rid of that line.

The style checker wants the last line of the file to end with the newline character, and to have no trailing whitespace on that line.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

I got the same error below when using Flake8:

no newline at end of fileFlake8(W292)

Because I didn't add one blank line after the last code print(math.pi) as shown below:

1 import math
2
3 print(math.pi)

So, I added one blank line after the last code print(math.pi) as shown below, then the error was solved:

1 import math
2
3 print(math.pi)
4

Actually, I saw PEP 8 but I could not find why to add one blank line after the last code but the error below also occurred in the same situation when using Pylint:

Final newline missingPylint(C0304:missing-final-newline)

So, I think that adding one blank line after the last code is recommanded in Python even though I don't know the reason.

In addition, if you add more than one blank lines after the last code print(math.pi) as shown below:

1 import math
2
3 print(math.pi)
4
5

Then, you get the errors below with Flake8 and Pylint respectively:

blank line at end of fileFlake8(W391)

Trailing newlinesPylint(C0305:trailing-newlines)

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
  • 1
    FYI, a newline character is *not* a “blank line” (nor is it a “new line”). At least not according to the [POSIX definition of a line](https://stackoverflow.com/a/729795/1968), which is what Python is using. Windows (and some online tools…) simply do not adhere to this convention, and this causes the conflict, and is the reason for the warning: as far as Python is concerned, your file (in particular its last line) is *incomplete*. – Konrad Rudolph Jun 10 '23 at 12:30