15

I have two computers with Windows, and I just found that on one of them, if I ran python code directly, like:

test_args.py input1 input2

Python will not recognized the input I gave, but this works:

python test_args.py input1 input2

I tried the code:

import sys

print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)

And the first way(test_args.py input1 input2) returns:

Number of arguments: 1 arguments.
Argument List: ['D:\\Test\\args\\test_args.py']

While the sceond way (python test_args.py input1 input2) returns:

Number of arguments: 3 arguments.
Argument List: ['D:\\Test\\args\\test_args.py', 'input1', 'input2']

Any idea on what this could happen? This issue only happens in one of my computers, both have same version of Windows.

Thanks!

SOLVED:

I search in regedit keyword "python" and found two keys missing %* after "C:\Python27\python.exe" "%1":

Computer\HKEY_CLASSES_ROOT\Applications\python.exe

Computer\HKEY_CLASSES_ROOT\py_auto_file\shell\open\command

And .py is associated with py_auto_file even though I tried assoc .py Python.File

Changing the two keys fixed this issue, thanks!

GooDeeJAY
  • 1,681
  • 2
  • 20
  • 27
Zewei Song
  • 521
  • 2
  • 6
  • 13
  • maybe you have different versions of python installed in both. Try running `python -V` to confirm the version numbers. – Ashoka Lella Apr 09 '15 at 14:14
  • Somehow you lost the proper file association. The default for Python 2.7 is `assoc .py=Python.File` and `ftype Python.File="C:\Python27\python.exe" "%1" %*`. – Eryk Sun Apr 09 '15 at 15:13
  • assoc .py=Python.File did not change the association of .py for some reason. Just found out that the value of .py under HKEY_CLASSES_ROOT\.py is py_auto_file for some reason. Changing the value in py_auto_file solve this issue, thanks! – Zewei Song Apr 10 '15 at 03:11
  • `HKCR` is a merged view of `HKLM\Software\Classes` (local machine) and `HKCU\Software\Classes` (user). The user settings in the `HKCU` hive takes precedence in the merged view. But the cmd shell's `assoc` and `ftype` commands only edit the `HKLM` settings. – Eryk Sun Apr 10 '15 at 03:29
  • 1
    Yes, `py_auto_file` and `pyw_auto_file` are the culprits. I just did an anaconda install on a brand new Windows 8 machine. Setting all the `assoc` and `ftype` values didn't work, but after fixing these two values everything is good. – Janus May 27 '15 at 06:19
  • Thank you for this solution! It saved a lot of my time. In my case the registry configuration was slightly different. On the PC1 (where it worked) there was neither HKEY_CLASSES_ROOT\Applications\python.exe nor HKEY_CLASSES_ROOT\py_auto_file, however there was HKEY_CLASSES_ROOT\Python.File\shell\open\command with a standard property with the value "C:\Python27\python.exe" "%1" %*. On the PC2 (there it didnt work) there was HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command with the wrong value and HKEY_CLASSES_ROOT\py_auto_file\shell\open\command file the wrong value as well. – Alexander Samoylov Apr 02 '19 at 12:09
  • I have corrected only the HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command on the PC2 and it started working (no console reopen was required). I also removed the second parameter HKEY_CLASSES_ROOT\py_auto_file and convinced that it just had no effect. – Alexander Samoylov Apr 02 '19 at 12:10

1 Answers1

1

Check what is the association of *.py files on these two computers. The script may be executed by different Python interpreters.

  • Both of my computers use Anaconda. I've made sure that the associations are the same. But it did not solve the issue. – Zewei Song Apr 09 '15 at 16:25
  • @ZeweiSong, there are way many locations where file type associations can be set. Check `HKLM\Software\Classes`, `HKLM\Software\Classes\Applications`, and `HKLM\Software\RegisteredApplications`, and the same subkeys under the per-user `HKCU` hive. Also check `HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts`. Refer to [File Types and File Associations](https://msdn.microsoft.com/en-us/library/cc144104%28v=vs.85%29.aspx). – Eryk Sun Apr 10 '15 at 01:12
  • @eryksun I found two keys missing the %*, guess that is for the sys input – Zewei Song Apr 10 '15 at 03:12
  • @ZeweiSong, `"%1"` is the script path, and `%*` is the rest of the arguments. – Eryk Sun Apr 10 '15 at 03:30