Search code examples
windowspowershellpython-poetrymicrosoft-store

Unrecognized command after poetry's installation on Windows 11


I never had troubles with the official installation of Poetry on Ubuntu systems. Now I'm trying to install it on Windows 11 in which there is Python 3.10. I've used the Power Shell commando as explaned here:

(Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | python -

After that I've checked poetry:

poetry --version
  • poetry --version
  • + CategoryInfo          : ObjectNotFound: (poetry:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
    

On JetBrains' site there is a guide to install poetry and I've used the command below at the end of installation:

$Env:Path += ";C:\Users\massi\AppData\Roaming\Python\Scripts"; setx PATH "$Env:Path"

But nothing is changed. Any suggestions?


Solution

  • Given your post-installation attempt to both update the in-process Path environment variable as well as its persistent definition, via setx.exe, the implication is that the poetry.exe executable is not located in C:\Users\massi\AppData\Roaming\Python\Scripts, despite what the installation instructions state, which is indeed the case:

    • The fact that you're piping to python rather than to py implies that you're using a Microsoft Store / winget-installed version of Python.

    • This version effectively redirects to a different, non-obvious location in the $env:LOCALAPPDATA directory subtree (not under $env:APPDATA, as the official instruction state) during the installation:

      • Its ultimate location is hinted at in what the installation process outputs, under Actual location.

      • One option is therefore to try to parse this output in order to glean the relevant location, but it's simpler to discover the true location of poetry.exe after the fact - see below.

    # Discover the true location of poetry.exe, via $env:LOCALAPPDATA
    $actualDir = 
      (
        Get-ChildItem $env:LOCALAPPDATA -Recurse -Filter poetry.exe -ErrorAction Ignore
      ).FullName[-1] | Split-Path
    
    # Add it to the in-process copy of the Path environment variable
    $env:Path += ";$actualDir"
    
    # This should now succeed.
    poetry --version
    

    As for persistently updating the Path environment variable:

    • Note that setx Path "$env:Path;$actualDir" is best avoided for persistent updates to the Path environment variable(s): while it may have no (immediate) ill effects, it can: setx.exe has a hard 1024-character limit, and invariably replaces the original REG_EXPAND_SZ value with a REG_SZ value, which means that basing entries on other environment variables no longer works. Additionally, you're duplicating entries by basing your new value on $env:Path, which is a composite of the system-level and the user-level definition.
      See this answer for more information, which also offers a custom Add-Path function for robust, side effect-free updating.

    • If you want to update the persistent Path definition manually, run $actualDir | Set-Clipboard in order to copy the directory path to the clipboard, then run sysdm.cpl, click on the Advanced tab, button Environment Variables..., and add the path on the clipboard to either the user-level or the system-level definition of Path (the latter requires administrative privileges).