DEV Community

Dendi Handian
Dendi Handian

Posted on • Updated on

Pyenv in Windows

Python is my first programming 'crush' since high school until college forced me to love PHP. Now I want to look back at Python and see the growth since then. The first obvious thing to do is installing the python interpreter on my (windows) machine. But I've heard of a tool that can switch between python versions. This mechanism is useful if something unexpected happened to like the installed version is too recent but we also need it for creating new fresh projects. The case for NodeJS technology is Node Version Manager (NVM) and I think Pyenv is the equivalent tool for Python. So let's try it instead of installing a normal python interpreter.

Installing Pyenv-win using Powershell (the easiest way)

Open and run as administrator a new powershell instance. Following this reference, Enable the execution policy first if you haven't:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
Enter fullscreen mode Exit fullscreen mode

Then install the pyenv-win using this command:

Invoke-WebRequest -UseBasicParsing -Uri "https://raw.githubusercontent.com/pyenv-win/pyenv-win/master/pyenv-win/install-pyenv-win.ps1" -OutFile "./install-pyenv-win.ps1"; &"./install-pyenv-win.ps1"
Enter fullscreen mode Exit fullscreen mode

Open new Command Prompt or Powershell (without administrator) to started using Pyenv-win

Installing Pyenv-win using Chocolatey

Make sure you already installed Chocolatey in your machine. If not, then see the official instruction to install chocolatey. As instructed in the installation guide, we can install Pyenv using Chocolatey by opening PowerShell terminal with Administrator mode and execute the command:

choco install pyenv-win
Enter fullscreen mode Exit fullscreen mode

If you are running Windows 10 1905 or newer, you might need to disable the built-in Python launcher via Start > "Manage App Execution Aliases" and disabling the "App Installer" aliases for Python.

After it successfully installed, try to close the PowerShell terminal and open it again with normal mode. Try to execute pyenv --version to check the installation.

If you see the pyenv version outputted, then it was OK. If not, try to manually configure the PATH by following the instruction.

These are what makes it works in my machine:

2020-10-26-15h06-55

Installing and use a Python version to Pyenv

To check any available version that can be installed, execute the command:

pyenv install --list
Enter fullscreen mode Exit fullscreen mode

You should see the list of versions that can be installed. In my case, the 3.8.3 was the latest, and let's install it by command:

pyenv install 3.8.3
Enter fullscreen mode Exit fullscreen mode

Installing this will output the progress in the CLI and open up an installer GUI. Follow and finish the installation.

To make sure the version was installed, check it with:

pyenv versions
Enter fullscreen mode Exit fullscreen mode

If it was there, then we can use it as a global and local interpreter by executing these:

pyenv global 3.8.3
Enter fullscreen mode Exit fullscreen mode
pyenv local 3.8.3
Enter fullscreen mode Exit fullscreen mode

The important thing after we change the version and setting the global and local interpreter, we have to execute this inside your home directory:

pyenv rehash
Enter fullscreen mode Exit fullscreen mode

Then when you execute python in CLI, you should be inside the python shell already.

Adding another version

Adding another version is exactly doing the previous steps but with the different versions. Try to install the other version using pyenv install <version>, then set global and local using pyenv global <version> and pyenv local <version>, don't forget to pyenv rehash to apply the change.

Troubleshoots

Packages doesn't use the current python's pip

The title might confuse you, but here is my case: I have installed 3.8.3 and installed pipenv on it with version 2020.8.*, but when I installed and switch to python 3.6.3 along with installing pipenv with a newer version 2020.11.* the pipenv executable still use the 2020.8.&. This was caused by the path value for executable python still use the version 3.8.3:

Alt Text

So, you can tackle this problem by manually set to use Python36.


Have fun exploring Pyenv.

Top comments (2)

Collapse
 
marinseremet12 profile image
Marin Seremet

hello !
I tried python 2.7, but I can't install pip.
What s the cause ?

errors are like this.
ERROR: Could not find a version that satisfies the requirement pip<21.0 (from versions: none)
ERROR: No matching distribution found for pip<21.0

Collapse
 
dendihandian profile image
Dendi Handian

Why still use python 2? Everything will be deprecated in python 2