Windows is not passing command line arguments to Python programs executed from the shell

37,581

Solution 1

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

Solution 2

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.

Solution 3

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.

Solution 4

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

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

Share:
37,581
mckoss
Author by

mckoss

I'm an ex-Microsoft developer (19 years). I worked on the original Excel development team, was the Outlook development manager, and started the team that became Microsoft SharePoint. My current favorite languages/environments are JavaScript and TypeScript. I work at Google on the Firebase team.

Updated on July 08, 2022

Comments

  • mckoss
    mckoss almost 2 years

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