0

When I run the following file directly through the command line

# winargs.py
import sys
print(sys.argv)

by placing that file in a directory in PATH and running the command winargs.py in either Window Command Prompt or Powershell, I get an unexpected trailing empty string.

['winargs.py', '']

I would expect it to output only the script name like so:

['winargs.py']

Windows Configuration

I previously could not pass arguments at all before but I followed the instructions here to add a "%*" to the HKEY_CLASSES_ROOT\Python.File\shell\open\command registry key and I can now pass arguments so running winargs.py a b 1 2 outputs ['winargs.py', 'a', 'b', '1', '2']. I am using the Anaconda distribution of Python for Windows and had that Python set as my Default Program for running *.py files. The registry key I modified after setting the default is

[HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command]
"C:\Users\Me\Anaconda3\python.exe"  "%1" "%*"

Larger Problem with argparse

This is causing larger difficulties when running scripts written with argparse that only have required positional arguments. Instead of printing a help message, it interprets that empty string as the first positional argument. For example, for the program

# winargparse.py
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("first_arg")
parser.parse_args()

I see no output running the command winargparse.py from both the Command Prompt and PowerShell, but expect it to print the default argparse help message as it does when I run python winargparse.py which looks like

usage: winargparse.py [-h] first_arg
winargparse.py: error: the following arguments are required: first_arg

How can I get Windows Command Prompt to run my Python files both without arguments and with arguments?

  • Possible duplicate: https://stackoverflow.com/questions/7574453/shebang-notation-python-scripts-on-windows-and-linux – AChampion Jul 13 '18 at 06:02
  • It is a side effect of adding `%*` in registry. It makes Windows to call python with at least one argument for your python program even it is an empty string in case of no arguments. – Alexander Ushakov Jul 13 '18 at 06:03
  • @AlexanderUshakov Is there some other wildcard so that it passes no empty string when there is nothing following the script name? – James Cagalawan Jul 13 '18 at 06:05
  • @JamesCagalawan As long as I know, anything you pass in command line will be treated as argument. Can you show full registry record - may be you use unneeded quotas in it around `%*`? – Alexander Ushakov Jul 13 '18 at 06:11
  • @AlexanderUshakov It is `"C:\Users\Me\Anaconda3\python.exe" "%1" "%*"`, I've also updated the question with this information. – James Cagalawan Jul 13 '18 at 06:19
  • @JamesCagalawan Try to remove quotas around `%*` as they are not present in answer for https://stackoverflow.com/questions/2640971 – Alexander Ushakov Jul 13 '18 at 06:23
  • Thanks @AlexanderUshakov, I really appreciate it. That did the trick, please submit as an answer if you have time so I can accept it. – James Cagalawan Jul 13 '18 at 06:26
  • You are welcome. Ii is great that you’ve solved your problem. – Alexander Ushakov Jul 13 '18 at 06:31

1 Answers1

2

You have unneeded double quotes around %* in your registry record. They make Windows pass an empty parameter if there are no arguments. Just remove these quotes.

Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
Alexander Ushakov
  • 5,139
  • 3
  • 27
  • 50