Windows 不会将命令行参数传递给从 shell 执行的 Python 程序
如果我尝试从 Windows 命令 shell 直接将命令行参数作为可执行命令执行,则在将命令行参数传递给 Python 程序时遇到问题。例如,如果我有这个程序(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" %*
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:]
And execute:
>test foo
Args: []
as compared to:
>python test.py foo
Args: ['foo']
My configuration has:
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" %*
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
我想我解决了这个问题。由于某种原因,注册表中有第二个位置(除了存储在 HKEY_CLASSES_ROOT\Python.File\shell\open\command 中的文件关联所示的位置之外):
这似乎是我系统上的控制设置。上面的注册表设置添加了“%*”以将所有参数传递给 python.exe(由于某种原因它在我的注册表中丢失了)。
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):
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 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"
,但这是我更改的使其起作用的键。For Python 3.3 on Windows 7, my setting was under another registry key; the key I changed to make the arguments get passed was
HKEY_USERS\S-1-5-21-3922133726-554333396-2662258059-1000_Classes\py_auto_file\shell\open\command
It was
"C:\Python\Python33\python.exe" "%1"
. I only appended%*
to it. The key's value is now"C:\Python\Python33\python.exe" "%1" %*
.I had several (at least five) other keys with the value
"C:\Python\Python33\python.exe" "%1"
, but this is the one I changed that made it work.我的设置位于另一个注册表项
HKEY_CLASSES_ROOT\py_auto_file
下。提到的其他键也存在,但 Windows 由于某种原因使用了这个键。My setting was under yet another registry key,
HKEY_CLASSES_ROOT\py_auto_file
. The other keys mentioned also existed, but Windows was using this one for some reason.为了让它为我工作,我必须使用注册表路径:
并添加
%*
To make it working for me, I had to use the registry path:
and added a
%*
以下是要修复 Python 3.6、2.7 和 Anaconda3 的 .reg 文件:
python-3.6.0.reg
python-2.7.0.reg
ananconda3.reg< /em>(更改用户名)
Here are .reg files to fix for Python 3.6, 2.7 and Anaconda3:
python-3.6.0.reg
python-2.7.0.reg
ananconda3.reg (change username)
如果在我的 Windows 10 系统上通过编辑以下注册表项来修复此问题:
为此值:
If fixed this on my Windows 10 system by editing the following registry keys:
to this value:
非常感谢大多数其他答案帮助我找到解决方案!
我的情况是使用 py.exe 打开 .py 文件(不是直接使用 python.exe),这种情况在几个例子中都提到过评论,但我决定将其作为单独的答案发布以强调差异。
所以我有与
C:\Windows\py.exe
关联的.py
文件,并在C:\Windows\py.ini
配置中我有几个 shebang 定义可以在我的脚本中使用,例如
#!
。在 Microsoft Windows 7 [版本 6.1.7601] 上,我的 python 脚本没有收到这样的参数,
但这工作正常
文件关联没问题
我已经尝试了很多注册表更改,但最后的帮助是以下更改(保存到
.reg
文件并运行)。我发现此注册表项正在搜索具有初始值"C:\Windows\py.exe" "%1"
的"%1"
字符串,并添加了% *
最后作为其他答案注意:有关信息,在我尝试设置这些键和值之前并没有帮助(至少在上面提到之前):
A lot of thanks for the most of other answers for helping me to find the solution!
My case was to open
.py
-files withpy.exe
(notpython.exe
directly), this case it noted in a couple of comments, but I decided to post this as a separate answer to emphasize the difference.So I have my
.py
-files associated withC:\Windows\py.exe
and inC:\Windows\py.ini
config I have a couple of shebang definitionsto use in my scripts like this
#!<MY_VENV_PY>
.And on Microsoft Windows 7 [Version 6.1.7601] my python script did NOT received the args like this
but this worked fine
File associations were OK
I've tried much of registry changes, but the last helped was the following change (saved to a
.reg
-file and run). I've found this registry key searching"%1"
string with initial value"C:\Windows\py.exe" "%1"
and added%*
in the end as other answers note:For information, before I tried to setup these keys and values and did not helped (at least before the noted above):
必须在 Windows 10 中修改它才能使其正常工作(%* 在末尾)
Computer\HKEY_USERS\S-1-5-21-2364940108-955964078-1358188674-1001\Software\Classes\Applications\py.exe\shell \打开\命令
Had to modify this in Windows 10 to get it to work (%* at the end)
Computer\HKEY_USERS\S-1-5-21-2364940108-955964078-1358188674-1001\Software\Classes\Applications\py.exe\shell\open\command
有趣的。使用 python 2.6 和 Windows XP (5.1.2600) 在这里工作:
Interesting. Works here using python 2.6 and Windows XP (5.1.2600):
您的
.py
文件的程序关联可能会混乱。只需将.py
文件与您的 python 可执行文件重新关联即可。右键单击
.py
文件 >打开方式
>选择默认程序...
> [找到C:\PythonXY\python.exe]Your program associations for
.py
files might be messed up. Just re-associate.py
files with your python executable.Right click a
.py
file >Open with
>Choose default program ...
> [find C:\PythonXY\python.exe]我使用
python.exe
和py_auto_file
检查了所有注册表项,并使它们指向我当前的 python 安装,包括末尾的%*
论据。他们有不少:但这并没有完成工作我。我还必须更改我的默认 python 应用程序。
正如大家所看到的,我安装了 3 个 Python 版本。在这里不可能看出哪个是哪个,所以我尝试了所有三个作为我的默认 python 应用程序。最终我能够用这三个之一获得我的脚本参数。
I checked all registry keys with
python.exe
andpy_auto_file
and made them point to my current python installation including th%*
at the end that passes arguments. They were quite a few:But that didn't do the job for me. I had to change my default python application as well.
As one can see I have 3 Python versions installed. It is impossible to see which is which here so I tried all three of them as my default python application. Eventually I was able to get my script arguments with one of these three.
通过查看 Windows 注册表,我发现了所有类似的地方
出现
Python36\pythonw.exe "%1" %*
。当我在命令提示符下输入 python app.py args 时,一切正常。
当我仅使用应用程序名称 (
app.py args
) 时,Windows 在 Python 中打开 app.py,但应用程序在尝试访问 argv[1] 时失败,因为 len(argv) 为 1。显然 Windows 知道足以将 py 文件传递给 Python,但我无法通过查看注册表项来弄清楚它是如何构造命令的。它似乎使用的是
"%1"
而不是"%1" %*
。By looking through the Windows registry, I found all the places where anything like
Python36\pythonw.exe "%1" %*
appears.When I type
python app.py args
at the command prompt, everything works properly.When I use just the app name (
app.py args
) Windows opens app.py in Python, but the app fails when it tries to access argv[1], because len(argv) is 1.Apparently Windows knows enough to pass a py file to Python, but I can't figure out from looking at registry entries how it constructs the command. It appears to be using
"%1"
rather than"%1" %*
.