4

I have some convenience python scripts in my path that I want to run by calling them directly, e.g. myscript.py argument1. But the arguments won't show up in sys.argv.

test.py

#!python3.5

import sys
print(sys.argv)

So when I call this little script directly I can not access the given argument via sys.argv.

c:\Users\MrLeeh\bin>test.py hello
['C:\\Users\\MrLeeh\\bin\\test.py']

While when I call it explicitly with the python command I can access my argument.

c:\Users\MrLeeh\bin>python test.py hello
['C:\\Users\\MrLeeh\\bin\\test.py', 'hello']

I ask myself what is the reason for this and how can I access the arguments when using a direct call. My python files are associated with py.exe.

MrLeeh
  • 5,321
  • 6
  • 33
  • 51

1 Answers1

6

Maybe you have missed some registry configuration.

There is a registry saved in HKEY_CLASSES_ROOT\Python.File\shell\open\command:

[HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command]
@="\"C:\\Python27\\python.exe\" \"%1\" %*"

The registry setting above adds the "%*" to pass all arguments to python.exe

Community
  • 1
  • 1
Dulguun
  • 554
  • 5
  • 11