Windows不会将命令行parameter passing给从shell执行的Python程序

如果我试图直接从Windows命令行执行它们作为可执行命令,那么我无法获得传递给Python程序的命令行参数。 例如,如果我有这个程序(test.py):

import sys print "Args: %r" % sys.argv[1:] 

并执行:

 >test foo Args: [] 

相比于:

 >python test.py foo Args: ['foo'] 

我的configuration有:

 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" %* 

我想我解决了这个问题。 出于某种原因,在注册表中有一个SECOND位置(除了存储在HKEY_CLASSES_ROOT \ Python.File \ shell \ open \ command中的文件关联所显示的位置):

 [HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command] @="\"C:\\Python25\\python.exe\" \"%1\" %*" 

这似乎是我的系统上的控制设置。 上面的注册表设置添加了“%*”将所有参数传递给python.exe(由于某种原因,在我的注册表中缺少这个参数)。

我的设置还在另一个注册表项HKEY_CLASSES_ROOT\py_auto_file 提到的其他键也存在,但由于某种原因,Windows正在使用这个键。

对于Windows 7上的Python 3.3,我的设置是在另一个注册表项下; 我改变了让参数通过的关键是

HKEY_USERS\S-1-5-21-3922133726-554333396-2662258059-1000_Classes\py_auto_file\shell\open\command

它是"C:\Python\Python33\python.exe" "%1" 我只附加了%* 现在键值是"C:\Python\Python33\python.exe" "%1" %*

我有几个(至少五个)其他键的值"C:\Python\Python33\python.exe" "%1" ,但这是我改变了它的工作。

为了使它为我工作,我不得不使用注册表路径:

 HKEY_CLASSES_ROOT\py_auto_file\shell\open\command 

并添加了一个%*

有趣。 在这里使用python 2.6和Windows XP(5.1.2600):

 C:\Documents and Settings\hbrown>python test.py foo ['test.py', 'foo'] C:\Documents and Settings\hbrown>test.py foo ['C:\\Documents and Settings\\hbrown\\test.py', 'foo'] C:\Documents and Settings\hbrown>test foo ['C:\\Documents and Settings\\hbrown\\test.py', 'foo'] C:\Documents and Settings\hbrown>type test.py import sys print sys.argv C:\Documents and Settings\hbrown>echo %PATHEXT% .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.PY C:\Documents and Settings\hbrown>assoc .py .py=Python.File 

这里有.reg文件来修复Python 3.6,2.7和Anaconda3:

蟒蛇,3.6.0.reg

 Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\.py] @="Python.File" "Content Type"="text/plain" [HKEY_CLASSES_ROOT\.pyc] @="Python.CompiledFile" "Content Type"="text/plain" [HKEY_CLASSES_ROOT\.pyw] @="Python.NoConFile" "Content Type"="text/plain" [HKEY_CLASSES_ROOT\py_auto_file] [HKEY_CLASSES_ROOT\py_auto_file\DefaultIcon] @="C:\\Python36\\DLLs\\py.ico" [HKEY_CLASSES_ROOT\py_auto_file\shell\open\command] @="\"C:\\Python36\\python.exe\" \"%1\" %*" [HKEY_CLASSES_ROOT\Python.File] @="Python File" [HKEY_CLASSES_ROOT\Python.File\DefaultIcon] @="C:\\Python36\\DLLs\\py.ico" [HKEY_CLASSES_ROOT\Python.File\shell\open\command] @="\"C:\\Python36\\python.exe\" \"%1\" %*" [HKEY_CLASSES_ROOT\Python.CompiledFile] @="Compiled Python File" [HKEY_CLASSES_ROOT\Python.CompiledFile\DefaultIcon] @="C:\\Python36\\DLLs\\pyc.ico" [HKEY_CLASSES_ROOT\Python.CompiledFile\shell\open\command] @="\"C:\\Python36\\python.exe\" \"%1\" %*" [HKEY_CLASSES_ROOT\Python.NoConFile] @="Python File (no console)" [HKEY_CLASSES_ROOT\Python.NoConFile\DefaultIcon] @="C:\\Python36\\DLLs\\py.ico" [HKEY_CLASSES_ROOT\Python.NoConFile\shell\open\command] @="\"C:\\Python36\\python.exe\" \"%1\" %*" 

蟒蛇,2.7.0.reg

 Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\.py] @="Python.File" "Content Type"="text/plain" [HKEY_CLASSES_ROOT\.pyc] @="Python.CompiledFile" "Content Type"="text/plain" [HKEY_CLASSES_ROOT\.pyw] @="Python.NoConFile" "Content Type"="text/plain" [HKEY_CLASSES_ROOT\py_auto_file] [HKEY_CLASSES_ROOT\py_auto_file\DefaultIcon] @="C:\\Python27\\DLLs\\py.ico" [HKEY_CLASSES_ROOT\py_auto_file\shell\open\command] @="\"C:\\Python27\\python.exe\" \"%1\" %*" [HKEY_CLASSES_ROOT\Python.File] @="Python File" [HKEY_CLASSES_ROOT\Python.File\DefaultIcon] @="C:\\Python27\\DLLs\\py.ico" [HKEY_CLASSES_ROOT\Python.File\shell\open\command] @="\"C:\\Python27\\python.exe\" \"%1\" %*" [HKEY_CLASSES_ROOT\Python.CompiledFile] @="Compiled Python File" [HKEY_CLASSES_ROOT\Python.CompiledFile\DefaultIcon] @="C:\\Python27\\DLLs\\pyc.ico" [HKEY_CLASSES_ROOT\Python.CompiledFile\shell\open\command] @="\"C:\\Python27\\python.exe\" \"%1\" %*" [HKEY_CLASSES_ROOT\Python.NoConFile] @="Python File (no console)" [HKEY_CLASSES_ROOT\Python.NoConFile\DefaultIcon] @="C:\\Python27\\DLLs\\py.ico" [HKEY_CLASSES_ROOT\Python.NoConFile\shell\open\command] @="\"C:\\Python27\\python.exe\" \"%1\" %*" 

ananconda3.reg (更改用户名)

 Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\.py] @="Python.File" "Content Type"="text/plain" [HKEY_CLASSES_ROOT\.pyc] @="Python.CompiledFile" "Content Type"="text/plain" [HKEY_CLASSES_ROOT\.pyw] @="Python.NoConFile" "Content Type"="text/plain" [HKEY_CLASSES_ROOT\py_auto_file] [HKEY_CLASSES_ROOT\py_auto_file\DefaultIcon] @="C:\\Users\\username\\Anaconda3\\DLLs\\py.ico" [HKEY_CLASSES_ROOT\py_auto_file\shell\open\command] @="\"C:\\Users\\username\\Anaconda3\\python.exe\" \"%1\" %*" [HKEY_CLASSES_ROOT\Python.File] @="Python File" [HKEY_CLASSES_ROOT\Python.File\DefaultIcon] @="C:\\Users\\username\\Anaconda3\\DLLs\\py.ico" [HKEY_CLASSES_ROOT\Python.File\shell\open\command] @="\"C:\\Users\\username\\Anaconda3\\python.exe\" \"%1\" %*" [HKEY_CLASSES_ROOT\Python.CompiledFile] @="Compiled Python File" [HKEY_CLASSES_ROOT\Python.CompiledFile\DefaultIcon] @="C:\\Users\\username\\Anaconda3\\DLLs\\pyc.ico" [HKEY_CLASSES_ROOT\Python.CompiledFile\shell\open\command] @="\"C:\\Users\\username\\Anaconda3\\python.exe\" \"%1\" %*" [HKEY_CLASSES_ROOT\Python.NoConFile] @="Python File (no console)" [HKEY_CLASSES_ROOT\Python.NoConFile\DefaultIcon] @="C:\\Users\\username\\Anaconda3\\DLLs\\py.ico" [HKEY_CLASSES_ROOT\Python.NoConFile\shell\open\command] @="\"C:\\Users\\username\\Anaconda3\\python.exe\" \"%1\" %*" 

.py文件的程序关联可能会混乱。 只需重新关联.py文件与您的python可执行文件。

右键单击.py文件> Open with > Choose default program ... > [找到C:\ PythonXY \ python.exe]

我用python.exepy_auto_file检查了所有的注册表项,并且让它们指向我当前的python安装,包括传递参数的末尾的%* 他们不少:

  • HKEY_CLASSES_ROOT \应用程序\ python.exe \壳\开放\命令:

    • org:“C:\ miniconda3 \ python.exe”“%1”“%*”
    • 改为:“C:\ Python35 \ python.exe”“%1”“%*”
  • HKEY_CLASSES_ROOT \ py_auto_file \壳\开放\命令

    • org:“C:\ Program Files \ Sublime Text 3 \ sublime_text.exe”“%1”
    • 改为:“C:\ Python35 \ python.exe”“%1”“%*”
  • HKEY_CURRENT_USER \ Software \ Classes下\ py_auto_file \壳\开放\命令

    • org:“C:\ Python35 \ python.exe”“%1”“%*”
  • HKEY_USERS \ S-1-5-21-2621213409-1291422344-4183577876-2165 \ Software \ Classes下\ py_auto_file \壳\开放\命令

    • org:“C:\ Python35 \ python.exe”“%1”“%*”
  • HKEY_USERS \ S-1-5-21-2621213409-1291422344-4183577876-2165_Classes \ py_auto_file \壳\开放\命令

    • org:“C:\ Python35 \ python.exe”“%1”“%*”
  • HKEY_CLASSES_ROOT \应用程序\ pythonw.exe \壳\开放\命令

    • org:“C:\ Python34 \ pythonw.exe”“”%1“
    • 已更改:“C:\ Python35 \ pythonw.exe”“%1”“%*”
  • HKEY_CURRENT_USER \ Software \ Classes下\应用程序\ python.exe \壳\开放\命令

    • org:“C:\ Python35 \ python.exe”“%1”“%*”

但是这对我来说并不合适。 我不得不改变我的默认python应用程序。

应用对话框

正如我们可以看到,我已经安装了3个Python版本。 这是不可能的,看看这是哪里,所以我尝试了所有三个作为我的默认python应用程序。 最终,我能够用这三者之一得到我的脚本参数。