Pyenv in Windows
Tags: pyenv, python, windows, chocolatey
Published: 2020-08-22 16:25:52

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](https://github.com/coreybutler/nvm-windows)) and I think [Pyenv](https://github.com/pyenv-win) 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](https://github.com/pyenv-win/pyenv-win/blob/master/docs/installation.md#powershell), Enable the execution policy first if you haven't:

```bat
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
```

Then install the pyenv-win using this command:

```bat
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"
```

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](https://chocolatey.org/install) to install chocolatey. As instructed in the [installation guide](https://github.com/pyenv-win/pyenv-win#installation), we can install Pyenv using Chocolatey by opening PowerShell terminal with Administrator mode and execute the command:

```bat
choco install pyenv-win
```

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](https://github.com/pyenv-win/pyenv-win#finish-the-installation).

These are what makes it works in my machine:

<img src="https://i.ibb.co/MGkQqtg/2020-10-26-15h06-55.png" alt="2020-10-26-15h06-55" border="0">

## Installing and use a Python version to Pyenv

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

```bat
pyenv install --list
```

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:

```bat
pyenv install 3.8.3
```

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:

```bat
pyenv versions
```

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

```bat
pyenv global 3.8.3
```

```bat
pyenv local 3.8.3
```

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

```bat
pyenv rehash
```
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](https://dev-to-uploads.s3.amazonaws.com/i/hgw1zo1aj1a317vist34.png)

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


__________

Have fun exploring Pyenv.