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 (
-vvv
option). -
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: open
- Created 4 years ago
- Reactions: 20
- Comments: 18 (7 by maintainers)
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:
Probably wrong. This issue is still valid. One could debate whether it is a bug or a missing feature (+ documentation bug).
I don’t understand why
poetry env use <version>
doesn’t just usepy --list-paths
to find the path to the desired python version on a Windows machine.This command…:
… gives this output on my machine:
It appears that
py
is 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 usepython
anymore. I always usepy
followed by a version and a command, for example:I have exactly the same issue, its quite annoying to be honest.
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 use
to usepy --list-paths
on 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).