poetry: poetry env use X.Y fails on Windows
-
I am on the latest Poetry version.
-
I have searched the issues of this repo and believe that this is not a duplicate.
-
If an exception occurs when executing a command, I executed it again in debug mode (
-vvvoption). -
OS version and name: Windows 10
-
Poetry version: 1.0.5
-
Link of a Gist with the contents of your pyproject.toml file: not needed
Issue
The shortcut command poetry env use X.Y to select the environment for Python X.Y, as documented in Managing environments, is broken in Windows. One is required instead to find, copy and paste the full path to Python executable.
Note that the documentation even says “especially Windows where pyenv is not available”.
>poetry env use 3.7
'python3.7' is not recognized as an internal or external command,
operable program or batch file.
The reason is quite simple: CPython installation in Windows doesn’t provide pythonX.Y.exe, so even if you have all of them in PATH, Poetry can only find the first one. They do, however, provide registry keys with installation locations, which Poetry can use to resolve X.Y to the Python interpreter path.
This piece of code should find the path to Python, given the version number as a X.Y string:
import os
try:
import winreg
except ImportError:
import _winreg as winreg
def find_python_interpreter(version):
for hkey, prefix in (
(winreg.HKEY_CURRENT_USER, "Software"),
(winreg.HKEY_CURRENT_USER, "Software\\WOW6432Node"),
(winreg.HKEY_LOCAL_MACHINE, "Software"),
(winreg.HKEY_LOCAL_MACHINE, "Software\\WOW6432Node"),
):
try:
value = winreg.QueryValue(hkey, "{}\\Python\\PythonCore\\{}\\InstallPath".format(prefix, version))
except FileNotFoundError:
continue
python = os.path.join(value, "python.exe")
if os.path.exists(python):
return python
return None
I have adjusted the code above to make it compatible with Python 2, but I have not tested afterwards, so please forgive any mistakes.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 22
- Comments: 30 (12 by maintainers)
Commits related to this issue
- fix(ci): use `python-path` for poetry env as X.Y not working on windows (python-poetry/poetry#2117) — committed to zyf722/execenv by zyf722 2 years ago
- Fixup https://github.com/python-poetry/poetry/issues/2117 — committed to synergyai-nl/chainlit by dokterbob 2 years ago
- Fixup https://github.com/python-poetry/poetry/issues/2117 — committed to synergyai-nl/chainlit by dokterbob 2 years ago
- Caching of Python deps in CI. Special care taken of https://github.com/python-poetry/poetry/issues/2117 — committed to synergyai-nl/chainlit by dokterbob 2 years ago
- Various improvements in tests/CI (#1271) * Run unit tests with all supported Python versions. * Caching of Python deps in CI. Special care taken of https://github.com/python-poetry/poetry/issues... — committed to Chainlit/chainlit by dokterbob 2 years ago
- Various improvements in tests/CI (#1271) * Run unit tests with all supported Python versions. * Caching of Python deps in CI. Special care taken of https://github.com/python-poetry/poetry/issues... — committed to Oleh929216/AI-Chainlit by Oleh929216 2 years ago
In my opinion this is a bug and it’s still present on poetry 1.4.2.
The introduction page says “It is multi-platform and the goal is to make it work equally well on Linux, macOS and Windows.” It should be possible for Windows users to use the same commands.
Same here, poetry 1.5.1, all python versions in Path, Windows10:
but this one is working:
People express their surprise in different ways. I agree that the previous poster’s language was not optimal. And I agree that there is nothing to merge.
With that said, some surprise is warranted. The status right now is:
poetry env use X.Yis broken on Windows if Python was installed with the official installer from python.org.poetry env use X.Ycommand without any warnings that it doesn’t work in Windows.I would create a pull request myself, but I haven’t figured out from where in the poetry code I would need to call the function. The exception, which is shown to the user when X.Y is not found seems to originate from another part of the code than the one I would think was used by poetry env.
So right now I am contemplating creating a pull request for a documentation update telling the user that the command is broken.
Probably wrong. This issue is still valid. One could debate whether it is a bug or a missing feature (+ documentation bug).
Thanks to #10097, this will work on Windows in Poetry 2.1.
I don’t understand why
poetry env use <version>doesn’t just usepy --list-pathsto find the path to the desired python version on a Windows machine.This command…:
… gives this output on my machine:
It appears that
pyis a rather underrated tool. Its official name is “Python Launcher for Windows”, and it comes with every python version since 3.6 (well, every version for Windows computers, that is). It is compatible with older versions too, if just one of the newer versions is installed. I never usepythonanymore. I always usepyfollowed by a version and a command, for example:I have exactly the same issue, its quite annoying to be honest.
@allanolesen there is
pythonfinderandfindpython. Those are 2 separate libraries.There is a plan to adopt
findpython, which provides proper windows support afaik, some work to make that possible is happening at #9050.Well, it is actually so simple that the code is in the very first post in this issue. Though with a hardcoded vendor name, which may not be the best approach.
Anyway, I have also poked around. Here are my findings:
The installer from python.org…:
Installing from Windows Store…:
I don’t know if Microsoft Store will install the
pylauncher. It was already installed on the PC I tested on (running Windows 11 Home). But from what I have read, it probably will not be installed.The command
poetry env use X.Ywill find the python versions installed by Microsoft Store, but not the ones installed by the official installer from python.org.The command
py --list-pathswill find all versions. So it must be searching in more than one way.Merge what exactly? Also, please refrain from using foul language.
can we please merge this ffs?
I think I am using the version, which came with Python 3.12.0. But I couldn’t find a way to make it reveal its version, and it is installed in a separate directory.
I just checked the registration database, and all my installed Python versions from 3.8 to 3.12 have the PEP 514 entries here: Computer\HKEY_CURRENT_USER\SOFTWARE\Python\PythonCore\
But I only have 3.11 and 3.12 in my path. So I wonder if Pythonfinder only uses the path, though it claims to use the registration database.
It seems to me that for
poetry env useto usepy --list-pathson Windows would be a step forward. However, it is not always present, since it is not included with the builds of Python provided by the Microsoft Store packages of Python. (I think it can also always be deselected explicitly when installing Python on Windows in the usual way, but this is less significant because people probably don’t deselect it and, if they do, are likely aware that they have done so.)same issue here. Any progress on this one? currently on poetry 1.6.0 (Windows 11)
Not sure if we have to implement PEP 514 compliant searching by ourselves. It may be more sustainable to use an existing lib (e.g. pythonfinder).