Windows 不会将命令行参数传递给从 shell 执行的 Python 程序

发布于 2024-08-28 21:53:44 字数 593 浏览 5 评论 0原文

如果我尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

最初的梦 2024-09-04 21:53:44

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

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

这似乎是我系统上的控制设置。上面的注册表设置添加了“%*”以将所有参数传递给 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):

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

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).

深海夜未眠 2024-09-04 21:53:44

对于 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.

や三分注定 2024-09-04 21:53:44

我的设置位于另一个注册表项 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.

荭秂 2024-09-04 21:53:44

为了让它为我工作,我必须使用注册表路径:

HKEY_CLASSES_ROOT\py_auto_file\shell\open\command

并添加 %*

To make it working for me, I had to use the registry path:

HKEY_CLASSES_ROOT\py_auto_file\shell\open\command

and added a %*

水晶透心 2024-09-04 21:53:44

以下是要修复 Python 3.6、2.7 和 Anaconda3 的 .reg 文件:

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

python-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< /em>(更改用户名)

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

Here are .reg files to fix for Python 3.6, 2.7 and Anaconda3:

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

python-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 (change username)

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\" %*"
奈何桥上唱咆哮 2024-09-04 21:53:44

如果在我的 Windows 10 系统上通过编辑以下注册表项来修复此问题:

Computer\HKEY_CLASSES_ROOT\py_auto_file\shell\open\command
Computer\HKEY_CLASSES_ROOT\Python.File\Shell\Open\Command
Computer\HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command

为此值:

"C:\Python27\python.exe" "%1" %*

If fixed this on my Windows 10 system by editing the following registry keys:

Computer\HKEY_CLASSES_ROOT\py_auto_file\shell\open\command
Computer\HKEY_CLASSES_ROOT\Python.File\Shell\Open\Command
Computer\HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command

to this value:

"C:\Python27\python.exe" "%1" %*
只是在用心讲痛 2024-09-04 21:53:44

非常感谢大多数其他答案帮助我找到解决方案!

我的情况是使用 py.exe 打开 .py 文件(不是直接使用 python.exe),这种情况在几个例子中都提到过评论,但我决定将其作为单独的答案发布以强调差异。

所以我有与 C:\Windows\py.exe 关联的 .py 文件,并在 C:\Windows\py.ini 配置中我有几个 shebang 定义

[commands]
<my_venv_py> = C:\Programs\my_venv_py\Scripts\python.exe
<my_venv_py_w> = C:\Programs\my_venv_py\Scripts\pythonw.exe

可以在我的脚本中使用,例如 #!

在 Microsoft Windows 7 [版本 6.1.7601] 上,我的 python 脚本没有收到这样的参数,

script.py 1 2

但这工作正常

py script.py 1 2

文件关联没问题

> assoc .py
.py=Python.File

> ftype | grep Python
File STDIN:
Python.CompiledFile="C:\Windows\py.exe" "%1" %*
Python.File=C:\Windows\py.exe "%L" %*
Python.NoConFile="C:\Windows\pyw.exe" "%1" %*

我已经尝试了很多注册表更改,但最后的帮助是以下更改(保存到 .reg 文件并运行)。我发现此注册表项正在搜索具有初始值 "C:\Windows\py.exe" "%1""%1" 字符串,并添加了 % * 最后作为其他答案注意:

Windows Registry Editor Version 5.00

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

有关信息,在我尝试设置这些键和值之前并没有帮助(至少在上面提到之前):

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\.py]
@="Python.File"
[HKEY_CURRENT_USER\Software\Classes\.py]
@="Python.File"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.py]
@="Python.File"
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.py]
@="Python.File"

[HKEY_CLASSES_ROOT\py_auto_file]
@="Python File"
[HKEY_CLASSES_ROOT\py_auto_file\shell\open\command]
@="\"C:\\Windows\\py.exe\" \"%1\" %*"

[HKEY_CLASSES_ROOT\Python.File]
@="Python File"
[HKEY_CLASSES_ROOT\Python.File\Shell\Open\command]
@="\"C:\\Windows\\py.exe\" \"%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 with py.exe (not python.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 with C:\Windows\py.exe and in C:\Windows\py.ini config I have a couple of shebang definitions

[commands]
<my_venv_py> = C:\Programs\my_venv_py\Scripts\python.exe
<my_venv_py_w> = C:\Programs\my_venv_py\Scripts\pythonw.exe

to 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

script.py 1 2

but this worked fine

py script.py 1 2

File associations were OK

> assoc .py
.py=Python.File

> ftype | grep Python
File STDIN:
Python.CompiledFile="C:\Windows\py.exe" "%1" %*
Python.File=C:\Windows\py.exe "%L" %*
Python.NoConFile="C:\Windows\pyw.exe" "%1" %*

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:

Windows Registry Editor Version 5.00

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

For information, before I tried to setup these keys and values and did not helped (at least before the noted above):

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\.py]
@="Python.File"
[HKEY_CURRENT_USER\Software\Classes\.py]
@="Python.File"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.py]
@="Python.File"
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.py]
@="Python.File"

[HKEY_CLASSES_ROOT\py_auto_file]
@="Python File"
[HKEY_CLASSES_ROOT\py_auto_file\shell\open\command]
@="\"C:\\Windows\\py.exe\" \"%1\" %*"

[HKEY_CLASSES_ROOT\Python.File]
@="Python File"
[HKEY_CLASSES_ROOT\Python.File\Shell\Open\command]
@="\"C:\\Windows\\py.exe\" \"%1\" %*"

咋地 2024-09-04 21:53:44

必须在 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

何其悲哀 2024-09-04 21:53:44

有趣的。使用 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

Interesting. Works here using python 2.6 and 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
只有一腔孤勇 2024-09-04 21:53:44

您的 .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]

寄意 2024-09-04 21:53:44

我使用 python.exepy_auto_file 检查了所有注册表项,并使它们指向我当前的 python 安装,包括末尾的 %*论据。他们有不少:

  • HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command:

    • 组织:“C:\miniconda3\python.exe”“%1”“%*”
    • 已更改:“C:\Python35\python.exe”“%1”“%*”
  • HKEY_CLASSES_ROOT\py_auto_file\shell\open\command

    • org: "C:\Program Files\Sublime Text 3\sublime_text.exe" "%1"
    • 已更改:“C:\Python35\python.exe”“%1”“%*”
  • HKEY_CURRENT_USER\Software\Classes\py_auto_file\shell\open\command

    • 组织:“C:\Python35\python.exe”“%1”“%*”
  • HKEY_USERS\S-1-5-21-2621213409-1291422344-4183577876-2165\Software\Classes\py_auto_file\shell\open\command

    • 组织:“C:\Python35\python.exe”“%1”“%*”
  • HKEY_USERS\S-1-5-21-2621213409-1291422344-4183577876-2165_Classes\py_auto_file\shell\open\command

    • 组织:“C:\Python35\python.exe”“%1”“%*”
  • HKEY_CLASSES_ROOT\Applications\pythonw.exe\shell\open\command

    • 组织:“C:\Python34\pythonw.exe”“%1”
    • 已更改:“C:\Python35\pythonw.exe”“%1”“%*”
  • HKEY_CURRENT_USER\Software\Classes\Applications\python.exe\shell\open\command

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

但这并没有完成工作我。我还必须更改我的默认 python 应用程序。

应用程序对话框

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

I checked all registry keys with python.exe and py_auto_file and made them point to my current python installation including th %* at the end that passes arguments. They were quite a few:

  • HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command:

    • org: "C:\miniconda3\python.exe" "%1" "%*"
    • changed: "C:\Python35\python.exe" "%1" "%*"
  • HKEY_CLASSES_ROOT\py_auto_file\shell\open\command

    • org: "C:\Program Files\Sublime Text 3\sublime_text.exe" "%1"
    • changed: "C:\Python35\python.exe" "%1" "%*"
  • HKEY_CURRENT_USER\Software\Classes\py_auto_file\shell\open\command

    • org: "C:\Python35\python.exe" "%1" "%*"
  • HKEY_USERS\S-1-5-21-2621213409-1291422344-4183577876-2165\Software\Classes\py_auto_file\shell\open\command

    • org: "C:\Python35\python.exe" "%1" "%*"
  • HKEY_USERS\S-1-5-21-2621213409-1291422344-4183577876-2165_Classes\py_auto_file\shell\open\command

    • org: "C:\Python35\python.exe" "%1" "%*"
  • HKEY_CLASSES_ROOT\Applications\pythonw.exe\shell\open\command

    • org: "C:\Python34\pythonw.exe" "%1"
    • changed: "C:\Python35\pythonw.exe" "%1" "%*"
  • HKEY_CURRENT_USER\Software\Classes\Applications\python.exe\shell\open\command

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

But that didn't do the job for me. I had to change my default python application as well.

Application dialog

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.

十秒萌定你 2024-09-04 21:53:44

通过查看 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" %*.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文