Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
290 views
in Technique[技术] by (71.8m points)

Python Script does not take sys.argv in Windows

I have two computers with Windows, and I just found that on one of them, if I ran python code directly, like:

test_args.py input1 input2

Python will not recognized the input I gave, but this works:

python test_args.py input1 input2

I tried the code:

import sys

print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)

And the first way(test_args.py) returns:

Number of arguments: 1 arguments.
Argument List: ['D:\Test\args\test_args.py']

While the sceond way (python test_args.py input1 input2) returns:

Number of arguments: 3 arguments.
Argument List: ['D:\Test\args\test_args.py', 'input1', 'input2']

Any idea on what this could happen? This issue only happens in one of my computers, both have same version of Windows.

Thanks!

SOLVED:

I search in regedit keyword "python" and found two keys missing %* after "C:Python27python.exe" "%1":

ComputerHKEY_CLASSES_ROOTApplicationspython.exe

ComputerHKEY_CLASSES_ROOTpy_auto_fileshellopencommand

And .py is associated with py_auto_file even though I tried assoc .py Python.File

Changing the two keys fixed this issue, thanks!

question from:https://stackoverflow.com/questions/65929161/cant-get-vars-from-sys-argv-past-sys-argv0-in-cmd-without-calling-python-as

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Check what is the association of *.py files on these two computers. The script may be executed by different Python interpreters.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...