可执行的Python脚本不在Windows中使用sys.argv

Zew*_*ong 9 python windows cmd command-line-arguments

我有两台带有Windows的计算机,我刚刚在其中一台计算机上发现,如果我直接运行python代码,例如:

test_args.py input1 input2
Run Code Online (Sandbox Code Playgroud)

Python不会识别我给出的输入,但这有效:

python test_args.py input1 input2
Run Code Online (Sandbox Code Playgroud)

我试过这段代码:

import sys

print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)
Run Code Online (Sandbox Code Playgroud)

第一种方式(test_args.py)返回:

Number of arguments: 1 arguments.
Argument List: ['D:\\Test\\args\\test_args.py']
Run Code Online (Sandbox Code Playgroud)

虽然sceond方式(python test_args.py input1 input2)返回:

Number of arguments: 3 arguments.
Argument List: ['D:\\Test\\args\\test_args.py', 'input1', 'input2']
Run Code Online (Sandbox Code Playgroud)

关于这可能发生什么的任何想法?此问题仅发生在我的一台计算机上,两台都有相同版本的Windows.

谢谢!

解决了:

我在regedit关键字"python"中搜索,发现在"C:\ Python27\python.exe""%1"之后缺少%*的两个键:

电脑\ HKEY_CLASSES_ROOT \应用程序\ python.exe

电脑\ HKEY_CLASSES_ROOT\py_auto_file \壳\开放\命令

并且.py与py_auto_file相关联,即使我尝试了关联.py Python.File

更改两个键解决了这个问题,谢谢!

Myk*_*nko 1

检查这两台计算机上的 *.py 文件的关联是什么。该脚本可以由不同的Python解释器执行。