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: closed
  • Created 7 years ago
  • Reactions: 22
  • Comments: 30 (12 by maintainers)

Commits related to this issue

Most upvoted comments

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:

C:\Users\guill>py --list
 -V:3.11 *        Python 3.11 (64-bit)
 -V:3.10          Python 3.10 (64-bit)
 -V:3.9           Python 3.9 (64-bit)
 -V:3.8           Python 3.8 (64-bit)
poetry env use 3.9
Could not find the python executable python3.9
poetry env use py -3.9
The option "3" does not exist
poetry env use python3.9
Could not find the python executable python3.9

but this one is working:

poetry env use C:\Users\guill\AppData\Local\Programs\Python\Python39\python.exe
Creating virtualenv vectorbt-backtesting in C:\Users\guill\Documents\Python\vectorbt_backtesting\.venv

Merge what exactly? Also, please refrain from using foul language.

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:

  1. The command poetry env use X.Y is broken on Windows if Python was installed with the official installer from python.org.
  2. This was reported 4 years ago in this issue.
  3. The original post in this issue contains the code needed for looking up the install path on a Windows machine according to PEP514.
  4. The documentation still lists the poetry env use X.Y command without any warnings that it doesn’t work in Windows.
  5. This issue does not seem to be part of any roadmap. It looks entirely abandoned. There is even a post where dimbleby asked if he could close it.

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.

#4615 was closed on the grounds that it was a duplicate.

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 use py --list-paths to find the path to the desired python version on a Windows machine.

This command…:

py --list-paths

… gives this output on my machine:

-V:3.12          C:\prog\Python\Python312\python.exe
-V:3.11          C:\Prog\Python\Python311\python.exe
-V:3.10          C:\prog\Python\Python310\python.exe
-V:3.9           C:\prog\Python\Python39\python.exe
-V:3.8           C:\prog\Python\Python38\python.exe

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 use python anymore. I always use py followed by a version and a command, for example:

py -3.11 -m venv ./.venv

I have exactly the same issue, its quite annoying to be honest.

@allanolesen there is pythonfinder and findpython. Those are 2 separate libraries.

In the meantime, I think it might make more sense to just implement it in Poetry.

There is a plan to adopt findpython, which provides proper windows support afaik, some work to make that possible is happening at #9050.

Late edit: I lean towards either using pep514tools directly, or just open-coding a PEP-514 consumer implementation; “Find a Python binary by that name in the registry” should be fairly simple.

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…:

  • WILL add the PEP514 keys to the registry
  • will NOT install an executable named pythonX.Y.exe
  • will NOT add the python install folder to the path unless the user marks a checkbox

Installing from Windows Store…:

  • will NOT add the PEP514 keys to the registry
  • WILL install an executable named pythonX.Y.exe
  • NOT add the python install folder to the path, but the above mentioned .exe will be installed in a folder, which already is in the path: C:\Users%username%\AppData\Local\Microsoft\WindowsApps\

I don’t know if Microsoft Store will install the py launcher. 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.Y will find the python versions installed by Microsoft Store, but not the ones installed by the official installer from python.org.

The command py --list-paths will find all versions. So it must be searching in more than one way.

can we please merge this ffs?

Merge what exactly? Also, please refrain from using foul language.

can we please merge this ffs?

Which version of py are you using?

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 use py --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).