Managing Python Versions on Windows with pyenv-win

Monday, January 18, 2021

Inspired by Matt Broberg’s post outlining the right way to install python on MacOS, this is the approach I use to manage python versions on Windows.

  1. Disable the existing app execution alias for python. By default, if you type python in the terminal and it’s not installed, it’ll prompt you to install it from the Microsoft Store. This isn’t the behavior we want, so we need to disable it. Hit the Windows Search icon and search for ‘Manage App Execution Aliases’ - after launching it, click on the two sliders for ‘App Installer’ marked ‘python.exe’ and python3.exe’ to the left to disable these.

Image of the app exeuction alias search result

  1. Next, we’re going to install pyenv-win. This allows us to manage python versions on the command line and set defaults globally, or even locally in specific projects. The [github repo]() for pyenv-win has several options to install; the one I prefer is to just execute git clone "$HOME/.pyenv" from a PowerShell prompt.

  2. Now, to add the appropriate environment variables, run the following in the PowerShell prompt - this is all documented at the repo above.

    [System.Environment]::SetEnvironmentVariable('PYENV',$env:USERPROFILE + "\.pyenv\pyenv-win\","User")
    [System.Environment]::SetEnvironmentVariable('PYENV_HOME',$env:USERPROFILE + "\.pyenv\pyenv-win\","User")
    [System.Environment]::SetEnvironmentVariable('path', $HOME + "\.pyenv\pyenv-win\bin;" + $HOME + "\.pyenv\pyenv-win\shims;" + $env:Path,"User")
  3. Restart your PowerShell Window and run pyenv - this should list the version and flags for pyenv. To see the list of available versions, run pyenv install -l. If you don’t have a specific version you need, I recommend picking the most current version of python 3 for your version of Windows. For 32-bit Windows, the appropriate versions have “-win32” at the end. Install that version with pyenv install $version where $version is the one you’re installing (ex: pyenv install 3.8.2).

  4. Select the version you just installed as the global version by running pyenv global $version where $version is the one you just installed (ex: pyenv global 3.8.2).

  5. Run pyenv rehash to update pyenv with the newly installed version.

That’s it! Following that, you’ll have the ability to use pyenv to manage multiple python versions on Windows. This is especially useful when you’re using other languages (such as Node) where modules may take a dependency on a specific python version.


Awesome, Lesser Known Azure CLI Extensions

PSReadLine for Light Mode Terminals

comments powered by Disqus