Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No arguments in sys.argv when calling Python Script directly on Windows [duplicate]

Tags:

python

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.

like image 893
MrLeeh Avatar asked Oct 17 '25 18:10

MrLeeh


1 Answers

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

  • via mckoss's answer on stackoverflow
like image 115
Dulguun Avatar answered Oct 20 '25 06:10

Dulguun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!