97

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" %*
denfromufa
  • 5,610
  • 13
  • 81
  • 138
mckoss
  • 6,764
  • 6
  • 33
  • 31
  • However it works for me. C:\Documents and Settings\Quim>test foo Args: ['foo'] I situated test.py in the initial location for cmd.exe (%HOMEDRIVE%%HOMEPATH% in properties). For the rest there is nothing special in my setup: winxp, Activepython 2.6.5 – joaquin Apr 14 '10 at 21:25
  • Could you please use [RegScanner](http://www.nirsoft.net/utils/regscanner.html) and find all occurrences of `Python.File` in Your registry? – Piotr Dobrogost Oct 22 '11 at 17:46
  • 2
    Wow what a frustrating bug :( I just reinstalled Python and it's still broken! The bug was reported at the Python issue tracker two years ago http://bugs.python.org/issue7936 but there's no fix - few of the devs use Windows. – Colonel Panic Sep 18 '12 at 19:38
  • 2
    Ran into this when I installed python 3.4 and tried to use python 2.7. Looks like the python 3.4 installer messed it up? – Dogmatixed Oct 21 '14 at 14:02
  • 3
    If the system is using `[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 the `shell\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" %*`. – Eryk Sun Jun 09 '16 at 06:54
  • 1
    cmd's internal `assoc` and `ftype` commands only show and modify `HKLM\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 in `HKCU\SOFTWARE\Classes`, which takes precedence over `HKLM` settings. Plus the association to the ProgId should always be managed via Explorer instead of using `assoc`. Explorer stores the user's choice in `HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts`. – Eryk Sun Jun 09 '16 at 07:02

12 Answers12

124

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

mckoss
  • 6,764
  • 6
  • 33
  • 31
  • This didn't work for me on Windows 7. What OS did you test this in? – Acorn May 26 '10 at 11:27
  • 8
    My key is located here on Windows 7: HKEY_USERS\S-1-5-21-2829634124-923609355-2255922086-1001\Software\Classes\Applications\python.exe\shell\open\command – Steven Keith Oct 08 '10 at 21:49
  • 4
    With the value: C:\Python26\python.exe %1 %* – Steven Keith Oct 08 '10 at 21:57
  • 1
    `HKEY_CLASSES_ROOT` is not the best place to check where given key is stored. See [where in the registry does Windows store, with which program to open certain file types?](http://superuser.com/questions/266268/) – Piotr Dobrogost Oct 22 '11 at 17:38
  • 2
    Worked for me, remember that mckoss's answer is escaped or a .REG file if you are editing the values directly removed the escapes in there – fedmich Mar 02 '14 at 11:03
  • Worked also for me under Windows 8. The Python 3.x installer did everything right at the beginning, but the problem arose when I added also the 2.x version. – 5agado Mar 19 '14 at 16:56
  • 2
    Thanks, it seems that python26 install (python-2.6.amd64.msi) is setting this badly. It fixed for me at your registry location with the exact following string: "C:\Python26\python.exe" "%1" %* – Pedro Reis Apr 01 '14 at 15:28
  • Note that depending on how you run this batch script, %1 may have to be changed to %%1 – denfromufa Mar 30 '15 at 02:20
  • 3
    On a side note: The root cause of this might be due to an initial file association with .py files prior to the Python install. Just ran into this issue, files of type .py had been associated with Notepad and then Python was installed and this ugly problem reared its ugly head. On Windows 10 BTW. – g4m3c0d3r May 24 '16 at 22:32
  • 3
    Recently changed the launcher to use C:\Windows\py.exe, and came across this bug. However, I modified the key in @StevenKeith's version, with "py.exe" substituted for "python.exe" – Casey Kuball Jun 21 '18 at 23:01
  • This was absolutely my problem. The behavior WORKED before, and somehow I did something that caused this change you mention to revert. No idea what made the change in the first place, nor what removed it. But THANK YOU! – Alexander Mays Oct 27 '21 at 18:47
14

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.

Cody Piersall
  • 8,312
  • 2
  • 43
  • 57
14

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.

Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76
bainorama
  • 331
  • 2
  • 6
5

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

rundekugel
  • 1,118
  • 1
  • 10
  • 23
5

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\" %*"
iki
  • 101
  • 1
  • 7
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" %*
Sociopath
  • 13,068
  • 19
  • 47
  • 75
stuw
  • 11
  • 2
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\" %*"

and1er
  • 749
  • 8
  • 18
1

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

Arunex
  • 13
  • 2
0

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
hughdbrown
  • 47,733
  • 20
  • 85
  • 108
0

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]

congusbongus
  • 13,359
  • 7
  • 71
  • 99
  • Did not work on my system, as it always defaulted to calling Python 3.6. The [selected answer](https://stackoverflow.com/a/2641185/5991423) did work – Aaron3468 May 23 '17 at 05:07
  • There are problems since Windows10, so this does not work always. – rundekugel Jan 14 '18 at 23:11
0

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.

MrLeeh
  • 5,321
  • 6
  • 33
  • 51
0

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