[Fixed]-Syntax error in a correct python script when redirected to stdin

1đź‘Ť

âś…

You’re having the opposite of the problem in the linked question. The -i makes python read the script in interactive mode (the “>>> ” and “… ” are prompts), and in interactive mode, you to press return (i.e. enter a blank line) to exit an indented section. The linked question involved blank lines in the middle of an indented section, which confused the interpreter. You have the opposite: you end an indented section without a blank line. That’s perfectly ok in non-intereactive mode, but in interactive mode it’s a no-no.

Solution: either put blank lines at the end of indented sections (and never in the middle), don’t tell the interpreter to read it in interactive mode. I’m not sure what you’re actually trying to accomplish with the -i flag, but note that python -i test.py (without the redirect) will run the script in non-interactive mode and then enter interactive mode and read from stdin.

Leave a comment