-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Script containing a "shebang" would not start under Windows #100107
Comments
The new launcher's "/usr/bin/env" virtual command searches If the command isn't found in
The launcher probably found the app execution alias "%LocalAppData%\Microsoft\WindowsApps\python3.exe". If the app version of Python 3 isn't installed, then by default Windows creates "python.exe" and "python3.exe" aliases for an "app installer" app that opens the store to install Python 3. If this app is passed any command-line arguments, it prints an informative message1 and exits. Since the app installer doesn't inherit a console from "pyw.exe", it allocates one to print the message. This console window gets destroyed as soon as the app exits2. If you don't have the app distribution of Python installed, I recommend disabling the "python.exe" and "python3.exe" app execution aliases.
For the virtual command "/usr/bin/env python", the launcher searches The old launcher reserves names that start with "python", even for the "/usr/bin/env" virtual command. In this case, "pyw.exe" runs a registered version of the GUI (non-console) binary, "pythonw.exe". Footnotes
|
Thank you for the explanation. So then it's a feature, not a bug, that my programs don't start after updating to 3.11 and I have to dive somewhere into the depths of the Windows configuration? I would probably do that, but I can't expect my non-technical audience to do that, with the best will in the world. If this isn't fixed, I'll have to modify my setup scripts to remove the "shebang" in Windows. For now, I'll go back to Python 3.9 and continue to advise against version 3.11. |
Yes, it's a feature, but one that's not currently implemented to be novice friendly considering the default configuration of Windows. It would be nice if the launcher detected and ignored the "python.exe" and "python3.exe" aliases for the app installer. I don't recall if the API has something to simplify this, but even if it doesn't, the launcher could just read the data in the reparse point to check whether the target app is "Microsoft.DesktopAppInstaller". If it finds the app installer, ignore the shebang and run the default Python.
Can you use "/usr/bin/python3" instead of "/usr/bin/env python3"? The former is restricted to registered installations and commands. It does not search |
Thank you for the advice. Unfortunately, I can't test it for now, since I just uninstalled Python 3.11.1, just as I did some weeks ago with 3.11, that already cost me a lot of time until I even found the source of the problem at the "shebang".
I guess that's true. However, "novice" does not quite fit the non-tech user group of my open source software. I don't expect them to learn Python or anything. I want to provide them with useful applications that will work without problems even after they might switch to Linux some day. However, for many of them it may already be a hurdle that they have to install a Python interpreter for this at all. |
This seems to do the trick. I have patched 24 projects so far. Thank you so much for your support. However, I give consideration to the fact that there might be a zillion Python scripts out there that suddenly "stop working" after updating to 3.11 because of the shebang. Not a good prospect. |
I agree. I'd prefer for the launcher to ignore the result of searching As to launching a GUI script with an attached console, that's not a showstopper, but it's also not correct behavior. The GUI script launcher could special case "python[X[.Y]]" to search for "pythonw[X[.Y]]". For your case, this would also avoid the pitfall of finding a "Microsoft.DesktopAppInstaller" alias. Windows doesn't create app installer aliases for "pythonw.exe" and "pythonw3.exe". More generally, the GUI script launcher could always use the process creation flag If a console application is launched that really requires a console, the spawned process may silently fail. A properly designed interpreter shouldn't crash if it has no console, but a script might die on an unhandled exception if it expects |
This a problem if you distribute your script to Mac users. This will use the default Python 3.9 install instead of any up-to-date installed versions. I believe BSDs have their python executables in A project I've worked on had no issues with just directing people to install the py launcher until the Windows Store version came around and broke things. Telling literally ever individual user to disable the app execution alias is draining on support volunteers. They're going to go with the alternate shebang right now but breaking BSD support and limiting the ability to use new language features is not ideal, and those are only the known issues. They're hoping this gets resolved before the problems end up as larger issues, but it doesn't really seem like this is actively trying to be resolved. |
This might be the only feasible option. I wish there were better APIs for managing these aliases, but Windows refuse to add them, and I'm starting to suspect they've quietly deprecated them entirely (in favour of what? I have no idea...). Needs someone to contribute it. And I'd only want it to happen in the context of the environment search - nobody else should have to pay the perf penalty. |
I don't have much experience with Windows development, but as a proof of concept I wrote this: https://github.com/python/cpython/compare/main...flagrama:cpython:py-launcher-shebang-appexeclink?expand=1 This seems to work how I would expect. If no Python is installed, I get an error about not being able to find a Python version. If I have just the store version installed it launches fine, as well if I have just a version from python.org installed. I don't know which one it uses when both are installed, but it still launches my script with a The code was written and tested in a fresh Windows 11 VM. The website version of python was 3.12.1 installed as a custom installation, the only optional features enabled were pip and py launcher and advanced options was only the Associate files with Python option. |
That's a great start, but the problem with that is we should use an actual alias to a real install, which your proposed change will also reject. Inside the inner condition we need to read the contents of the appexeclink and check which package the link belongs to. It will differ between the App Installer redirector and a PSF released Python install. Only the App Installer one should cause us to reject. |
This mess is what I managed: https://github.com/python/cpython/compare/main...flagrama:cpython:py-launcher-shebang-appexeclink?expand=1 Not pretty, probably inefficient, and no proper error handling, but it does what it needs to. |
…Microsoft Store (pythonGH-114358) (cherry picked from commit d5c21c1) Co-authored-by: Vincent Cunningham <[email protected]>
…Microsoft Store (pythonGH-114358) (cherry picked from commit d5c21c1) Co-authored-by: Vincent Cunningham <[email protected]>
…oft Store (GH-114358) (cherry picked from commit d5c21c1) Co-authored-by: Vincent Cunningham <[email protected]>
…oft Store (GH-114358) (cherry picked from commit d5c21c1) Co-authored-by: Vincent Cunningham <[email protected]>
Bug report
This Python script hello.pyw does not start on my PC when I double click it in Windows Explorer.
Instead, a console window pops up and disappears instantly.
Now I modify the shebang as follows:
On double-clicking hello.pyw, a console opens, then the message box appears.
Now I remove the shebang:
On double-clicking hello.pyw, the message box appears as expected.
This misbehavior was observed with the introduction of Python 3.11. It was not like this until version 3.10.8.
Your environment
I have implemented quite a few open source programs with Python, all of which have a shebang for working across platforms. On my download pages I had to generally discourage the use with Python 3.11.
Linked PRs
The text was updated successfully, but these errors were encountered: