0

Why isn't the launcher running the script with the specified version of Python?

Host is a Windows 10 machine with CPython 3.7 installed. Running a python script with the following contents...

#! /usr/bin/env python3

import sys

print(sys.version)
print(sys.path)

Yields the following...

2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:19:22) [MSC v.1500 32 bit (Intel)]
['C:\\PROGRA~2\\python27\\lib\\site-packages\\pars-0.1.0-py2.7.egg', 'C:\\Windows\\SYSTEM32\\python27.zip', 'C:\\PROGRA~2\\python27\\DLLs', 'C:\\PROGRA~2\\python27\\lib', 'C:\\PROGRA~2\\python27\\lib\\plat-win', 'C:\\PROGRA~2\\python27\\lib\\lib-tk', 'C:\\PROGRA~2\\python27', 'C:\\PROGRA~2\\python27\\lib\\site-packages']

Python 3.7 is in my path. Running python --version or py --version returns Python 3.7.3.

This is driving me nutz.

Adam
  • 3,891
  • 3
  • 19
  • 42
  • I wasn't aware Windows had support for shebangs. – Axe319 Oct 15 '20 at 10:55
  • 1
    https://stackoverflow.com/a/12716560/12479639 this might be helpful. Also, if you are only running python 3 scripts it may be helpful to associate `.py` files with that specific interpreter. – Axe319 Oct 15 '20 at 11:03
  • 1
    In Windows, `#! /usr/bin/env python3` currently does not search `PATH` for "python3.exe". Since there is no history of installing a "python3.exe" executable in Windows, this particular `env` case just uses the normal registry search for 3.x installations under "Software\Python\PythonCore\...". – Eryk Sun Oct 15 '20 at 11:27

1 Answers1

0

To resolve this problem, I had to fix the association of ".py" files and py.exe, which was broken on my machine.

NOTE: A user "ETalbot" provided the answer in the thread Should I put #! (shebang) in Python scripts, and what form should it take?. Credit to him for the answer (up-vote his answer). I am on a client's laptop. I don't have permission to make the registry change he suggested under HKEY_LOCAL_MACHINE (HKLM). I made it in HKEY_LOCAL_USER (HKLU) with the same effect. For folks in my situation, I'm adding the steps to make that change here.

Using Python on Windows includes the following guidance under the section 3.8.1.4. From file associations:

The launcher should have been associated with Python files (i.e. .py, .pyw, .pyc files) when it was installed. This means that when you double-click on one of these files from Windows explorer the launcher will be used, and therefore you can use the same facilities described above to have the script specify the version which should be used.

Set the registry key inside the following to "C:\Windows\py.exe" "%1" %*:

HKEY_CURRENT_USER\SOFTWARE\Classes\Python.File\shell\open\command

If you're not sure how to edit the registry on Windows, use the following guide:

  1. Create a file named "python.reg".
  2. Put the following stuff in the file.

NOTE: See Eryk Sun's comments on this post for guidance on the icon and drop file class identifier. Some of the values in here are specific to my system.


Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\SOFTWARE\Classes\Python.File]
@="Python File"

[HKEY_CURRENT_USER\SOFTWARE\Classes\Python.File\DefaultIcon]
@="C:\\PROGRA~2\\python27\\DLLs\\py.ico"

[HKEY_CURRENT_USER\SOFTWARE\Classes\Python.File\Shell]

[HKEY_CURRENT_USER\SOFTWARE\Classes\Python.File\Shell\Edit with IDLE]

[HKEY_CURRENT_USER\SOFTWARE\Classes\Python.File\Shell\Edit with IDLE\command]
@="\"C:\\PROGRA~2\\python27\\pythonw.exe\" \"C:\\PROGRA~2\\python27\\Lib\\idlelib\\idle.pyw\" -e \"%1\""

[HKEY_CURRENT_USER\SOFTWARE\Classes\Python.File\Shell\editwithidle]
"MUIVerb"="&Edit with IDLE"
"Subcommands"=""

[HKEY_CURRENT_USER\SOFTWARE\Classes\Python.File\Shell\editwithidle\shell]

[HKEY_CURRENT_USER\SOFTWARE\Classes\Python.File\Shell\editwithidle\shell\edit37]
"MUIVerb"="Edit with IDLE 3.7 (64-bit)"

[HKEY_CURRENT_USER\SOFTWARE\Classes\Python.File\Shell\editwithidle\shell\edit37\command]
@="\"C:\\Program Files\\Python37\\pythonw.exe\" -m idlelib \"%L\" %*"

[HKEY_CURRENT_USER\SOFTWARE\Classes\Python.File\Shell\open]

[HKEY_CURRENT_USER\SOFTWARE\Classes\Python.File\Shell\open\command]
@="\"C:\\Windows\\py.exe\" \"%1\" %*"

[HKEY_CURRENT_USER\SOFTWARE\Classes\Python.File\shellex]

[HKEY_CURRENT_USER\SOFTWARE\Classes\Python.File\shellex\DropHandler]
@="{60254CA5-953B-11CF-8C96-00AA00B8708C}"

  1. Launch regedit.
  2. Select Import... from the File menu.
  3. Select the file you created named "python.reg".
  4. Click Ok at the prompt, which should indicate success.

If you re-run the use case from my earlier post, you'll see it works now.

For reference, HKLU is consulted first then HKLM so things in HKLU supersede keys in HKLM. Also, as a user, you generally have access to HKLU since... you're the user :-)

User "ErykSun" had noted that (a) this issue could be resolved by repairing the installation, and (b) the path/call to py.exe wouldn't work for user (not system) scoped installations. While (a) wasn't an option for me due to permissions on the system, it's likely a safer alternative. And, Python is installed globally on my system, you might have to tweak the path to py.exe if you've installed Python differently.

Adam
  • 3,891
  • 3
  • 19
  • 42
  • 1
    The default icon should come from the launcher: `"C:\Windows\py.exe",1`. And the "DropHandler" should be `{BEA218D2-6950-497B-9434-61683EC065FE}`, which uses Python's "C:\Windows\pyshellext.amd64.dll" shell extension, which has proper Unicode support for dropped files. But first check that you have it defined under "HKLM\Software\Classes\CLSID". – Eryk Sun Oct 15 '20 at 11:37