Open12

Windowsに複数のPythonバージョンを管理する

豚&紙箱豚&紙箱
  1. Anaconda
  2. pyenv
  3. asdf

私のOS:Windows 11
Cディスクのスペースが限られているので、Python環境をDディスクにインストールしたいです。

豚&紙箱豚&紙箱

Anaconda

データサイエンティスト向けです。使い方はpipと似ています。

pipのライブラリリポジトリはPyPi、condaのライブラリリポジトリはconda-forgeです。conda-forgeが管理しているライブラリのバージョンはPyPiより古い可能性があります。一部のライブラリはconda-forgeにないかもしれません。

法人向けは有料化らしいです。

豚&紙箱豚&紙箱

pyenv

pyenvは公式にWindowsに対応していません。Windowsの場合はpyenv-winがおすすめです。
https://github.com/pyenv-win/pyenv-win

PowerShellでのインストール方法

一番簡単な方法です。

インストールする時に以下のエラーが出ました。

& : File D:\install-pyenv-win.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see
about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.]

解決方法:以下のPowerShellコマンドを実行します。

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

CurrentUserLocalMachineにすると管理者権限が必要になります。

この問題について公式の説明資料にも記載があります↓
https://github.com/pyenv-win/pyenv-win/blob/master/docs/installation.md#powershell

↓の記事も参考になります。
https://zenn.dev/is0383kk/articles/09e5fd89cbd30c

豚&紙箱豚&紙箱

pyenv-winをGitでのインストール方法

デフォルトの方法です。Gitは既にパソコンに入っているので、この方法で行こうと思います。
https://github.com/pyenv-win/pyenv-win/blob/master/docs/installation.md#git-commands

PowerShellで以下のコマンドを実行

PS D:\> git clone https://github.com/pyenv-win/pyenv-win.git pyenv

システム設定を追加
https://github.com/pyenv-win/pyenv-win/blob/master/docs/installation.md#add-system-settings

[System.Environment]::SetEnvironmentVariable('PYENV',"D:\pyenv\pyenv-win\","User")

[System.Environment]::SetEnvironmentVariable('PYENV_ROOT', "D:\pyenv\pyenv-win\","User")

[System.Environment]::SetEnvironmentVariable('PYENV_HOME',"D:\pyenv\pyenv-win\","User")

[System.Environment]::SetEnvironmentVariable(..., "Machine")なら、管理者権限が必要かもしれません。

[System.Environment]::SetEnvironmentVariable('path', "D:\pyenv\pyenv-win\bin;D:\pyenv\pyenv-win\shims;" + [System.Environment]::GetEnvironmentVariable('path', "User"),"User")

PowerShellの実行ポリシーを変更しない限り、「running scripts is disabled on this system」は必ず出ます。

やはり実行ポリシーを変更しないといけません。

豚&紙箱豚&紙箱

pyenv-winは一番簡単なインストール方法で、つまりinstall-pyenv-win.ps1をダウンロードし実行すると、${env:USERPROFILE}にインストールされます。

https://github.com/pyenv-win/pyenv-win/blob/e80393d3f76faebd9fbfb75ca452220b3bc57f78/pyenv-win/install-pyenv-win.ps1#L29

他のフォルダ(私の場合はD:\)にインストールしたい場合はやはりGitでのインストール方法がいいです。

もちろん、install-pyenv-win.ps1をダウンロードした後、中身を修正してから実行してもいいです。

豚&紙箱豚&紙箱

uv

https://github.com/astral-sh/uv

https://zenn.dev/turing_motors/articles/594fbef42a36ee

公式のInstallationに従って以下一行のコマンドラインでインストールできるようです。

powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"

でもそうしたらuvは~/.local/binにインストールされます。別のフォルダにインストールするために、UV_INSTALL_DIR環境変数を設定します。公式のChanging the installation pathを参考してください。

powershell -ExecutionPolicy ByPass -c {$env:UV_INSTALL_DIR = "D:\uv";irm https://astral.sh/uv/install.ps1 | iex}

豚&紙箱豚&紙箱

uvはpythonをデフォルトでCドライブにインストールします。

>dir C:\Users\zzxia\AppData\Roaming\uv\python\
 Volume in drive C is OS
 Volume Serial Number is F478-BAAD

 Directory of C:\Users\zzxia\AppData\Roaming\uv\python

06/29/2025  02:00 PM    <DIR>          .
06/28/2025  07:36 PM    <DIR>          ..
06/28/2025  07:36 PM                 1 .gitignore
06/28/2025  07:36 PM                 0 .lock
06/29/2025  02:00 PM    <DIR>          .temp
06/29/2025  02:00 PM    <DIR>          cpython-3.10.18-windows-x86_64-none
06/28/2025  07:36 PM    <DIR>          cpython-3.13.5-windows-x86_64-none
               2 File(s)              1 bytes
               5 Dir(s)  71,248,797,696 bytes free

>dir C:\Users\zzxia\AppData\Roaming\uv\python\cpython-3.10.18-windows-x86_64-none
 Volume in drive C is OS
 Volume Serial Number is F478-BAAD

 Directory of C:\Users\zzxia\AppData\Roaming\uv\python\cpython-3.10.18-windows-x86_64-none

06/29/2025  02:00 PM    <DIR>          .
06/29/2025  02:00 PM    <DIR>          ..
06/29/2025  01:59 PM    <DIR>          DLLs
06/29/2025  01:59 PM    <DIR>          include
06/29/2025  02:00 PM    <DIR>          Lib
06/29/2025  01:59 PM    <DIR>          libs
06/29/2025  01:59 PM            25,066 LICENSE.txt
06/29/2025  01:59 PM            91,648 python.exe
06/29/2025  01:59 PM            54,272 python3.dll
06/29/2025  02:00 PM         4,545,536 python310.dll
06/29/2025  02:00 PM            90,112 pythonw.exe
06/29/2025  01:59 PM    <DIR>          Scripts
06/29/2025  02:00 PM    <DIR>          tcl
06/29/2025  02:00 PM           103,936 vcruntime140.dll
06/29/2025  02:00 PM            29,184 vcruntime140_1.dll
06/29/2025  02:00 PM            17,920 vcruntime140_threads.dll
               8 File(s)      4,957,674 bytes
               8 Dir(s)  71,246,897,152 bytes free

これはuvで作った仮想環境の.venv/pyvenv.cfgファイルで知りました。]

home = C:\Users\zzxia\AppData\Roaming\uv\python\cpython-3.10.18-windows-x86_64-none
implementation = CPython
uv = 0.7.16
version_info = 3.10.18
include-system-site-packages = false
prompt = espnet-lab