If the operators and scripts are the same python script from the command line

Why am I getting a syntax error for the following single python liner 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 one line from the command line?

+7
python command-line bash shell
source share
4 answers

One way around this limitation is to specify a command with the format $'string' 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 invalid POSIX sh.

As @slaadvak mentioned, there are some other workarounds here: Running multi-line Python statements on a single line command line

+6
source share

The problem is not only with the import statement, but that you have something in front of the control flow statement. This will not work:

 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 link ( https://docs.python.org/2/reference/compound_stmts.html ), ';' can only be used to combine "simple statements" together. In this case, you combine the simple import re if True: with if True: if True not a simple statement because it introduces flow control and therefore is a composite statement. At least how I interpret the documentation.

Here's the full text from a Python link:

Compound statements consist of one or more β€œsentences”. The article consists of a headline and a set. The article headings of a particular compound statement are at the same level of indentation. Each paragraph heading begins with a uniquely identifying keyword and ends with a colon. A set is a group of conditionally controlled operators. A Suite can be one or more simple words with semicolon delimiters on the same line as the heading after a colon colon, or it can be one or more indentation in 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)* [";"] 
+4
source share

You can insert new lines directly into the argument.

 $ python -c 'import re > if True: > print "HELLO" > ' 
+1
source share

Why am I getting a syntax error for the following single python liner code?

Python gramma may prohibit small_stmt ';' compound_stmt small_stmt ';' compound_stmt . The -c argument is probably 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 new line at the end of simple_stmt . if_stmt not small_stmt it cannot follow another small_stmt after ';' . A new line is required to enter compound_stmt after small_stmt .

This is not a problem because bash allows multi-line arguments, just do not close the open single quote until you execute, for example:

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

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

+1
source share

All Articles