1

The community-accepted solution for including a shebang line for Python seems to be to use #!/usr/bin/env python3. However, when I install Python 3 using the winget package (i.e. winget install --id Python.Python.3.11), and then attempt to run a script with the shebang (i.e. py some_script_with_shebang.py), I get an error:

Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings

> Manage App Execution Aliases.

Through investigation, I found that I can install the msstore package to resolve this (winget install --id 9NRWMJP3717K, and where python3 returns %LOCALAPPDATA%\Microsoft\WindowsApps\python3.exe). However, I don't know why I need to install the msstore version (with the opaque ID), rather than the winget package (with the transparent ID).

Casey Kuball
  • 456
  • 5
  • 18
  • 1
    NOT A SOLUTION, but when I discovered this, I simply found the folder where `python.exe` was and `mklink /h python3.exe "C:\Program Files\Python39\python.exe"`. – Jeff Zeitlin Jan 13 '23 at 19:40
  • @JeffZeitlin that might be the best solution unfortunately. Apparently installing the `msstore` version on top of the other will not do what you want, as it just means now you have two versions, where `py -3` is one version and `python3` is another. With different packages installed. – Casey Kuball Jan 13 '23 at 20:05
  • The reason I say that it's not a solution is because there is the risk of a disk check reporting "cross-linked" files - one of many holdovers from the DOS/FAT12 days. It's also not something for someone unfamiliar with the command line to do. Certainly, someone with familiarity with UNIX and UNIX-like operating systems (including Linux) would consider this an almost trivial workaround - but it's still going to be somewhat foreign to "old DOS hands" with little *IX exposure. – Jeff Zeitlin Jan 13 '23 at 20:34
  • @JeffZeitlin I wouldn't think twice about making a link, I'm surprised that it would be an issue. Another alternative would just be making an alias. In powershell, `md %USERPROFILE%\Documents\WindowsPowerShell` > `notepad $profile` > paste `Set-Alias python3 path\to\python.exe` Fair point though on it not being for someone unfamiliar with command line, my question *was* meant to be along the lines of "why doesn't `python3` work out of the box?". – Casey Kuball Jan 13 '23 at 21:28
  • 1
    The alias would only work _in_ powershell, and I suspect that the python loader that handles shebangs probably won't invoke powershell by default. Another alternative would be to create `PYTHON3.CMD` in the same folder as `PYTHON.EXE`, with content `@python %*` – Jeff Zeitlin Jan 17 '23 at 12:35

1 Answers1

-1

Could it be that you didn't re-open your command prompt after winget install --id Python.Python.3.11? In that case, the new python wouldn't have been on your PATH yet. Normally, winget does place Python on the PATH, but it only takes effect in new command prompt windows. This is discussed for example in https://winget.pro/winget-install-python/.

  • `python` is on the path, but there doesn't exist any `python3`, with the winget package. I checked the install directory directly, this is not an "I forgot to restart my shell" problem. – Casey Kuball Aug 24 '23 at 16:33