8
votes

Why do I receive a syntax error for the following one liner python code?

python -c 'import re; if True: print "HELLO";'
  File "<string>", line 1
    import re; if True: print "HELLO";
                ^
SyntaxError: invalid syntax

The following code works just fine

python -c 'if True: print "HELLO";'

How can I change my one line to execute my intended script on a single line from the command line?

4
Not an answer: Why do you want to put stuff into one line? What is the benefit, why is it better than a real script? - guettli
The benefit is that I don't have any file write permissions on the system I am running this on. Also, I commonly want to copy and paste one liners to friends, and this makes it so they don't have to create files and grant those execute permissions. - AffluentOwl
This looks like being a duplicate of this : stackoverflow.com/questions/2043453/… At least, the link contains a lot of nice workarounds.. - slaadvak
@slaadvak I agree this is a duplicate of that question, but I like Rynants answer provided below better than the ones offered there. @ Ashkan That question answers why one liners cannot exist in a python file, but doesn't answer how to write one line python code on the command line, which is what I'm trying to do. - AffluentOwl

4 Answers

10
votes

One option to work around this limitation is to specify the command with the $'string' format using the newline escape sequence \n.

python -c $'import re\nif True: print "HELLO";'

Note: this is supported by shells such as bash and zsh, but is not valid POSIX sh.

As mentioned by @slaadvak, there are some other workarounds here: Executing Python multi-line statements in the one-line command-line

5
votes

The problem isn't with the import statement specifically, its that you have anything before a control flow statement. This won't work, either:

dan@dan:~> python -c 'a = "1234" ; if True: print "hi"'
  File "<string>", line 1
    a = "1234" ; if True: print "hi"
                  ^
SyntaxError: invalid syntax

According to the Python reference (https://docs.python.org/2/reference/compound_stmts.html), ';' can only be used to combine "simple statements" together. In this case you're combining the simple statement import re, with if True:. if True isn't a simple statement, because it's introducing flow control, and is therefore a compound statement. At least that's how I interpret the documentation.

Here's the full text from the Python reference:

Compound statements consist of one or more ‘clauses.’ A clause consists of a header and a ‘suite.’ The clause headers of a particular compound statement are all at the same indentation level. Each clause header begins with a uniquely identifying keyword and ends with a colon. A suite is a group of statements controlled by a clause. A suite can be one or more semicolon-separated simple statements on the same line as the header, following the header’s colon, or it can be one or more indented statements on subsequent lines

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | funcdef
                   | classdef
                   | decorated
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]
2
votes

You can embed newlines directly in the argument.

$ python -c 'import re
> if True:
>  print "HELLO"
> '
2
votes

Why do I receive a syntax error for the following one liner python code?

Python grammar might forbid small_stmt ';' compound_stmt. -c argument is probably is interpreted as file_input:

fileinput: (NEWLINE | stmt)* ENDMARKER
stmt: simple_stmt | compound_stmt
simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
small_stmt: import_stmt <in this case>
compound_stmt: if_stmt <in this case>

Note: there is a newline at the end of simple_stmt. if_stmt is not small_stmt it can't follow another small_stmt after ';'. A newline is necessary to introduce compound_stmt after small_stmt.

It is not an issue because bash allows multiline arguments, just don't close the opening single quote until you done e.g.:

$ python -c '
> import re
> if 1:
>   print(1)
> '
1

Note: > are printed by the shell itself here. It is not entered by hand.