1

I'm learning to include command line parameters in my code. I've read the docs for argparse and I tried running this script from there.

#argparse_trial.py

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))

If I run

>python argparse_trial.py 1

in the command line, I get the expected result 1
But if I run

>argparse_trial.py 1

I get

usage: arg_parse_trial.py [-h] [--sum] N [N ...]
argparse_trial.py: error: the following arguments are required: N

I checked and the only argument the code seems to receive in the second case is the filename itself, no matter how many arguments are given.
I'm on a Windows machine and python is in my path.

Why is the second case failing in this script? How can I get it to work?

9
  • I don't know how to phrase this question properly, so feel free to make it better Commented Jun 9, 2016 at 5:02
  • I'm not sure how you are able to execute a python script without the python executable. At least on Windows. Commented Jun 9, 2016 at 5:04
  • Like I said, python is in my path Commented Jun 9, 2016 at 5:05
  • But you're not typing its name, so the system will not look for it. Commented Jun 9, 2016 at 5:06
  • Well, how come the file is executed then? I run all my .py files without adding python before them and they've all worked properly till now Commented Jun 9, 2016 at 5:08

4 Answers 4

2

This is a guess, I cannot test right now, but I believe this is what is happening:

  1. you type the name of your python file.
  2. Windows fails to run the file as a program, so…
  3. Windows tries to open the file, using the associated program (kindof with using start).
  4. While doing so, it simply ignores other things on the line, and…
  5. …generates a basic command line for the python interpreter to use, featuring just the interpreter itself and the target file to open.

Think of what happens when you "run" a text document.

If the command line is not generated correctly, running this command should fix it (replace the path as appropriate):

ftype Python.File=C:\Path\to\python.exe "%1" %*

Notice the %* at the end. If it's amiss, arguments will be dropped.

Sign up to request clarification or add additional context in comments.

19 Comments

Thanks for the answer. But I run youtube-dl on a regular basis. I've never included python before it and it accepts a lot of command-line arguments
Can you check you python file association then? It should look like D:\Program Files\Python\python.exe "%1" %*. If the %* is missing, it could cause arguments to be dropped.
@GughanRavikumar> the settings that tell windows how to open a .py file.
yes, because these are all exe files and will work if you have path provided in the environment variable.
check C:/Python27/Scripts and see.
|
2

I think the code is working fine. You can execute the program by just choosing the default program to be python.exe for python files.
In your case python.exe "1" %* as @spectras suggested, so it will be able take command line arguments.

Also provide the path environment variable.
Programs like pip,virtualenv and youtube-dl are all executable files and if have already set the environment variable, we use it anywhere.

How to choose default program on windows:
http://windows.microsoft.com/en-in/windows/change-default-programs#1TC=windows-7

1 Comment

My default program is python.exe. It has to be python.exe "1" %*, thanks to @spectra. Just change that, I'll make yours the accepted answer
0

You can Use sys library included in python and use argv() function to perform various command argunment.

find this video tutorial intuitive to understand how to!

https://www.youtube.com/watch?v=rLG7Tz6db0w

Comments

0

This is in addition to spectras' and Bhansa's answer.
For Windows 10 user's, setting advanced file associations is a bit complicated. You'll have to edit your registry files. Run regedit.
Go to HKEY_CURRENT_USER\SOFTWARE\Classes\py_auto_file\shell\open\command and change the value of Data from
"C:\Path\To\Python\python.exe" "1"
to
"C:\Path\To\Python\python.exe" "1" %*

This might be applicable to previous versions of Windows also.

2 Comments

The configuration is FUBAR'd if .py files are using the automatically generated py_auto_file ProgId. Make sure that .py files are associated with Python.File. I would delete the py_auto_file entry and use Explorer's "open with" or the settings app to select the correct "Python" ProgId. If the command-line template is wrong, use regedit to modify Python.File in the user's HKCU (for an all-users installation, you can use cmd's assoc and ftype to modify the HKLM values). If Python 3 is installed, use the fully qualified path to the "py.exe" laucher (try where py.exe in cmd).
@eryksun I'm sorry, I didn't understand most of what you said. I've explained what I did to get it working on my system. Using explorer's open with you can't set advanced options, like %* So I had to edit the registry to get it working

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.