-1

I'm having trouble understanding what "No newline at end of file" means exactly.

I've got

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

The error is pointing to the last line

debug=False

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

wjandrea
  • 23,210
  • 7
  • 49
  • 68
Ryu
  • 1
  • 3
  • 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

1 Answers1

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.


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
  • 669,072
  • 92
  • 771
  • 1,162