mathsbeauty
mathsbeauty

Reputation: 366

Python program with arguments on Windows

How to run this program saved in the file test.py on Windows XP with python 2.7 installed.

import argparse
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',const=sum, default=max,help='sum the integers (default: find the max)')
args = parser.parse_args()
print args.accumulate(args.integers)

I tried to run it with command line. For example

$ python test.py 1 2 3 4

or

$ python test.py 1 2 3 4 --sum

gives error "invalid syntax".

Upvotes: 0

Views: 5988

Answers (3)

hpaulj
hpaulj

Reputation: 231395

Your test script is the first example on the Python argparse documentation. https://docs.python.org/3/library/argparse.html

Your comment with new lines added is

Python 2.7.8 (default, Jun 30 2014, 16:03:49) 
[MSC v.1500 32 bit (Intel)] on win32 
Type "copyright", "credits" or "license()" for more information. 
>>> ================================ RESTART ================================ 
>>> usage: test [-h] [--sum] N [N ...] test: error: too few arguments 
>>> $ python test.py 1 2 3 4 
SyntaxError: invalid syntax 
>>> python test.py 1 2 3 4 
SyntaxError: invalid syntax 
>>> $ python test.py 1 2 3 4 
SyntaxError: invalid syntax 
>>> python test.py 1 2 3 4 --sum 
SyntaxError: invalid syntax 
>>> python test.py 1 2 3 4 --sum

From this I deduce that you saved the script as test ('test.py` would have been better), and ran it, from a Windows command line, as

python -i test

which produces

usage: test [-h] [--sum] N [N ...] test: error: too few arguments

That usage message from the parser; test is the name of the script.

I'm not sure about the RESTART line. My tests (at the end) suggest your Python call (or some default environment feature) includes the -i option, which leaves you in the interactive Python session, even after the argparse step fails.

The next command is straight out of the Python example:

>>> $ python test.py 1 2 3 4 
SyntaxError: invalid syntax 

But the context is all wrong. The docs include $ to indicate that this is being typed in a commandline (Linux shell or Windows commmand). And the meaning, in the correct context is:

  • run Python
  • tell it to run the test.py script
  • and pass it the arguments '1','2', etc

But if you are already inside a Python interpreter (indicated by the >>> prompt string), this does not make sense. python and test.py are strings that don't have a default meaning inside Python. So the interpreter gives you a syntax error. And none of the variations fix that.

A little further along, the argparse documentation gives an example of calling a parser from within a Python interactive session:

>>> parser.parse_args(['--sum', '7', '-1', '42'])

That has a very different syntax. In this python -i context it should run.

Going back to the Windows command window and typing

python test 1 2 3 4

has a better chance of working. If that doesn't work, then you/we need to focus on running an even more basic Python script.

=========

Here's an example of running another simple script from a Linux shell. The ...$ is the shell prompt; the >>> is the python prompt. Adding the -i to the initial python call ensures it stays in python after parsing.

0957:~/mypy$ python -i simple.py
usage: simple.py [-h] foo
simple.py: error: too few arguments
Traceback (most recent call last):
  File "simple.py", line 4, in <module>
    print(parser.parse_args())
   ...
SystemExit: 2
>>> python simple.py 1 2
  File "<stdin>", line 1
    python simple.py 1 2
                ^
SyntaxError: invalid syntax

The main difference between my test and yours is that I don't get the RESTART and I get a traceback. Without the -i I simply get the usage message and a return the command line.

1000:~/mypy$ python simple.py
usage: simple.py [-h] foo
simple.py: error: too few arguments
1000:~/mypy$

Upvotes: 0

John1024
John1024

Reputation: 113844

I tried running your script at the command line and it works perfectly:

$ python arg.py 1 2 3 4 --sum
10

In the above, the $ is the shell's prompt. What I entered is python arg.py 1 2 3 4 --sum. It works.

Now, let's do what I suspect that you are doing: let's start an interactive python shell and enter the above:

$ python
Python 2.7.12+ (default, Aug  4 2016, 20:04:34) 
[GCC 6.1.1 20160724] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> python test.py 1 2 3 4 --sum 
  File "<stdin>", line 1
    python test.py 1 2 3 4 --sum 
              ^
SyntaxError: invalid syntax

This generates the SyntaxError: invalid syntax error that you see. (There is one minor difference: I am on Linux and you are on Windows.)

The solution is to exit the python interactive shell and enter the command at the command prompt.

Upvotes: 3

Mario
Mario

Reputation: 36487

This is just me being naive, but considering the short error message you posted...

Any chance you're getting this code off some book and try to run this on a command line?

The book uses $ to mark command line/terminal commands, but the character is actually not part of the syntax or command you're supposed to use.

So instead of running this:

$ python 1 2 3

Run this:

python 1 2 3

Upvotes: 0

Related Questions