Windows 没有将命令行参数传递给从 shell 执行的 Python 程序 [英] Windows is not passing command line arguments to Python programs executed from the shell
问题描述
如果我尝试从 Windows 命令外壳将命令行参数作为可执行命令直接执行,则无法获取传递给 Python 程序的命令行参数.例如,如果我有这个程序(test.py):
I'm having trouble getting command line arguments passed to Python programs if I try to execute them directly as executable commands from a Windows command shell. For example, if I have this program (test.py):
import sys
print "Args: %r" % sys.argv[1:]
并执行:
>test foo
Args: []
相比:
>python test.py foo
Args: ['foo']
我的配置有:
PATH=...;C:python25;...
PATHEXT=...;.PY;....
>assoc .py
.py=Python.File
>ftype | grep Python
Python.CompiledFile="C:Python25python.exe" "%1" %*
Python.File="C:Python25python.exe" "%1" %*
Python.NoConFile="C:Python25pythonw.exe" "%1" %*
推荐答案
我想我解决了这个问题.出于某种原因,注册表中有第二个位置(除了存储在 HKEY_CLASSES_ROOTPython.Fileshellopencommand 中的文件关联显示的位置):
I think I solved this. For some reason there is a SECOND place in the registry (besides that shown by the file associations stored in HKEY_CLASSES_ROOTPython.Fileshellopencommand):
[HKEY_CLASSES_ROOTApplicationspython.exeshellopencommand]
@=""C:\Python25\python.exe" "%1" %*"
这似乎是我系统上的控制设置.上面的注册表设置添加了%*"以将所有参数传递给 python.exe(由于某种原因它在我的注册表中丢失).
This seems to be the controlling setting on my system. The registry setting above adds the "%*" to pass all arguments to python.exe (it was missing in my registry for some reason).
这篇关于Windows 没有将命令行参数传递给从 shell 执行的 Python 程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!