Syntax Error When Using Command Line in Python

syntax error when using command line in python

Looks like your problem is that you are trying to run python test.py from within the Python interpreter, which is why you're seeing that traceback.

Make sure you're out of the interpreter, then run the python test.py command from bash or command prompt or whatever.

Syntax error when defining a function on the Python command line

Press enter once after defining your function (that is, enter one blank line). Essentially, this lets Python know that you are done defining your function.

Once you see >>> again, you can call your function.

See the picture for how it should look when done right:

Sample Image

SyntaxError: Invalid Syntax when trying to run a .py script from IDLE in Command Prompt

That's not a Python program, it's the log of an interactive (command prompt) session.

Instead, try entering the following in any text editor (e.g. notepad, notepad++), save it as C:\code\script2.py and then run it as you did:

import sys

print(sys.platform)

x="Spam!"
print(x*8)

print(2**100)

[EDIT]
If you want to use Idle for this, click [File][New] to create a Python source code file, type in the above, save it and then run it as you did.

[EDIT2]
Idle is and example of an Interactive Development Environment (IDE). Since you're new to programming: IDE's tend to obscure what's going on, although Idle isn't a severe case of this. So using a separate editor and running from the command line as you did is actually a good way to familiarize yourself with what's going on under the hood. This will pay off in many ways in the long run.

Running git command in Python script results in syntax error

Python script

(This section directly answers the question, meaning getting a Python script that accomplishes what the questioner wants to accomplish. However, it may be the case that a shell script is more appropriate; see the "Shell script" section for more on this.)

I have made a Python script that accomplishes what you want. I made this script by taking your script, and making two modifications:

  • I changed the intended double quotes around awk's only parameter to be escaped single quotes \'.
  • I changed the literal newline \n to be an escaped version of "\n", namely \\n.

Here is an example of output showing the modified script working:

$ git log  --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }'
added lines: 5, removed lines: 1, total lines: 4

$ cat script.py
import os
GitCommand = 'git log --pretty=tformat: --numstat | awk \'{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\\n", add, subs, loc }\''
report = os.system(GitCommand)

$ python3 script.py
added lines: 5, removed lines: 1, total lines: 4

The following shows a diff of the Python script, from the question's version to this answer's working version:

$ git diff head~ head --word-diff-regex=. script.py
diff --git a/script.py b/script.py
[...]
--- a/script.py
+++ b/script.py
@@ -1,3 +1,3 @@
import os
GitCommand = 'git log --pretty=tformat: --numstat | awk [-"-]{+\'+}{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: % , total lines: %s\{+\+}n", add, subs, loc }[-"-]{+\'+}'
report = os.system(GitCommand)

Shell script

As mentioned elsewhere in this question, having a shell script file may be the most appropriate way to simply and repeatedly invoke any given shell command that, for whatever reason, is not desired to be stored in something like ~/.bashrc, ~/.bash_profile, or similar.

Specifically for this question, here's an example:

$ git log  --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }'
added lines: 5, removed lines: 1, total lines: 4

$ cat ./total-lines.sh
git log --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }'

$ ./total-lines.sh
added lines: 5, removed lines: 1, total lines: 4

My Environment

$ systeminfo | grep --extended-regexp --regexp="^OS (Name|Version)"
OS Name: Microsoft Windows 10 Pro
OS Version: 10.0.19043 N/A Build 19043

$ bash --version | head --lines=1
GNU bash, version 4.4.23(1)-release (x86_64-pc-msys)

$ git --version
git version 2.33.0.windows.2

$ python3 --version
Python 3.9.7

$ awk --version | head --lines=1
GNU Awk 5.0.0, API: 2.0 (GNU MPFR 4.1.0, GNU MP 6.2.1)


Related Topics



Leave a reply



Submit