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\" %*"
[HKCU|HKLM]\SOFTWARE\Classes\Applications\python.exe
or[HKCU|HKLM\SOFTWARE\Classes\py_auto_file
, then it's misconfigured, and the offending keys should be deleted. Then use Explorer ("open with" or the file association settings app) to select the correct "Python" entry that uses the standard[HKCU|HKLM]\SOFTWARE\Classes\Python.File
ProgId. Check theshell\open\command
subkey in regedit. If Python 3 is installed, the command template should use the py launcher. If the launcher is installed for all users, the template should be"C:\Windows\py.exe" "%1" %*
.assoc
andftype
commands only show and modifyHKLM\SOFTWARE\Classes
, so they require administrator access to write to the registry, and they're not the correct tools to use if Python is installed for the current user. You have to use regedit to modify per-user settings inHKCU\SOFTWARE\Classes
, which takes precedence overHKLM
settings. Plus the association to the ProgId should always be managed via Explorer instead of usingassoc
. Explorer stores the user's choice inHKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts
.