# pyenv-win

- URL: <https://krymtkts.github.io/posts/2021-12-04-pyenv-win.html.md>
- Date: 2021-12-04
- Author: krymtkts
- Tags: python

Python の処理系をローカルに直で入れることに抵抗はなかったのだが、chocolatey で Python3 を入れていると pin でもしない限りバージョンが進んでいって処理系が乱雑に配置されるので、Python 自体のバージョン管理をすることにした。

[pyenv-win/pyenv-win: pyenv for Windows. pyenv is a simple python version management tool. It lets you easily switch between multiple versions of Python. It's simple, unobtrusive, and follows the UNIX tradition of single-purpose tools that do one thing well.](https://github.com/pyenv-win/pyenv-win)

事前に Windows が Microsoft Store アプリの Python を使わないようにしておく必要がある。これがクソめんどい。以下は `README.md` からの引用。

> NOTE: 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 turning off the "App Installer" aliases for Python

Win キーを押下して "Manage App Execution Aliases" をタイプするのが現状の最速と思うが、できたら `ms-settings` スキームで一発で飛べたら良いのに(`ms-settings:appsfeatures`で手前まで行くのが精一杯)。

わたしは chocolatey ユーザなので、 chocolatey でこの pyenv をインストールした。この場合環境変数の設定が chocolatey により行われるので幾分サボれる。

[Chocolatey Software | pyenv-win 2.64.11](https://community.chocolatey.org/packages/pyenv-win#psdsc)

わたしの場合は chocolatey 操作とゴミを消すのだけ管理者権限が必要なようにしてるのでそうしたが、それ以外は普通で行った。

```powershell
(pip freeze | ConvertFrom-StringData).Keys | Set-Content requirements.txt # モジュールを控える

choco uninstall python3 -y
rm C:/Python* -recurse -force # ゴミを消す
choco install pyenv-win -y

pyenv install 3.9.6 # まだ 3.10.0 がリリースキャンディデートしかなかったので
pyenv global 3.9.6
pyenv rehash # バージョン切り替え後に必要

pip --version # 確認
python -m pip install --upgrade pip # 更新してって出たので更新
pip install -r requirements.txt # モジュール復元
# done.
```

### バージョン移行時の `requirements.txt`

バージョン乗り換え時の `requirements.txt` の作成だが、単純に `pip freeze` の出力を使うとバージョン不整合で取り込めないことがある。
なのでわたしの場合は雑にモジュール名だけにフィルタリングしたものを使う。

```powershell
(pip freeze | ConvertFrom-StringData).Keys | Set-Content requirements.txt
```
