Windows没有将命令行参数传递给从外壳执行的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:\Python25\python.exe" "%1" %*
Python.File="C:\Python25\python.exe" "%1" %*
Python.NoConFile="C:\Python25\pythonw.exe" "%1" %*
推荐答案
我想我解决了这个问题。由于某种原因,注册表中存在第二个位置(除了存储在HKEY_CLASSES_ROOT\Python.File\shell\open\命令中的文件关联所显示的位置之外):
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_ROOT\Python.File\shell\open\command):
[HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command]
@="\"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没有将命令行参数传递给从外壳执行的Python程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!