Natively support `py` launcher on Windows
- [x] I have searched the issues of this repo and believe that this is not a duplicate.
- [x] I have searched the documentation and believe that my question is not covered.
Feature Request
poetry env use 3.9 is really nice, but assumes a binary named python3.9 is in the path.
On Windows, the Python installer does not install such version-named binaries, and so to avoid having multiple python.exe on the path, the py launcher is included, which is used by py -<version> exactly as Poetry is trying to do.
It would be nice if Poetry could correctly use this. I couldn't find a way to do this easily, as
poetry env use 'py -3.9'
tries to execute a binary named "py -3.9" (I suspect this is an intended regression due to fixing #1747), and without the quotes, it tries to take -3.9 as an option for itself, and fails.
As a workaround, I am, under PowerShell, doing this:
poetry env use $(py -3.8 -c 'import sys; print(sys.executable)')
which works fine.
Note that this isn't a problem for simply poetry install workflows, as it deafults to the Python with which Poetry is associated, and Poetry already knows the location of this binary. However, I hit this with a package that does not support Python 3.9, while I have Poetry installed under Python 3.9.
This problem also appears when trying to clean up venvs, as poetry env remove does not support "default Python" the way poetry install does.
poetry destroy was rejected in #926, so you can only clean up Poetry env by naming either the env's full name or the path to the Python binary; the latter requires the same workaround I'm using above.
I wonder if it might be better to use a lib that implements PEP-514 (e.g. pythonfinder) to find executables instead of using an application like py launcher?
Related-to: #2117
Yeah, that would make sense too, since py.exe has been rewritten in Python 3.11 as a PEP-514-consuming tool, so the results should be consistent.
Any workaround for now? Is there a way to switch the default python version for a session for example?
@eyadmba the workaround is to specify the full path to the python.exe. It can be either the path to the one in the Python installation folder or the one in the Poetry environment, it doesn't matter. You can either use the following command, as indicated by @TBBle:
As a workaround, I am, under PowerShell, doing this:
poetry env use $(py -3.8 -c 'import sys; print(sys.executable)')
Or, if you have multiple environments already, one trick I sometimes do is to type poetry env info, copy the path, paste it in front of poetry env use and change the version number.
I'm closing this ticket in favour of #2117, since with Python 3.11's py consuming the PEP-514 data, I think it'd be better to consume that directly than try and wrap py or similar. #2117 is also a more problem-oriented ticket, this one lead with the solution.
It might be nice to have a way to make poetry env use 'py -3.9' work, i.e. be able to specify the Python binary and args for it, but that's a differently complex issue, and not Windows-specific.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.