LoginSignup
53
42

【pyenv-win】pyenv のインストールと実行

Last updated at Posted at 2021-09-26

はじめに

pyenv-win: Windows 用の pyenv

(記事中の引用はすべて上記ページの README.md から)

環境

  • Microsoft Windows 10.0.19042.1165
  • PowerShell 5.1.19041.1237
  • Git 2.33.0.windows.2

pyenvとは

pyenv is a simple python version management tool. It lets you easily switch between multiple versions of Python.
(訳:pyenv はシンプルな Python のバージョン管理ツールであり、Python の複数のバージョンを簡単に切り替えることができる。)

インストール

次のいずれかの方法で pyenv-win をインストールする

pip を使用

Powershell または Git Bash の場合

pip install pyenv-win --target $HOME\\.pyenv

cmd.exe の場合

pip install pyenv-win --target %USERPROFILE%\.pyenv

zip ファイルを使用

  1. ダウンロードリンク:pyenv-win

  2. .pyenv ディレクトリを $HOME 下、存在しない場合は %USERPROFILE% 下に作成

  3. ファイルを取り出して次の場所に移動する
    Powershell または Git Bash の場合 $HOME/.pyenv/
    cmd.exe の場合 %USERPROFILE%\.pyenv\

  4. bin フォルダが %USERPROFILE%\.pyenv\pyenv-win 下にあることを確認

Git を使用

Powershell または Git Bash の場合

git clone https://github.com/pyenv-win/pyenv-win.git "$HOME/.pyenv"

cmd.exe の場合

git clone https://github.com/pyenv-win/pyenv-win.git "%USERPROFILE%\.pyenv"

Chocolatey を使用

choco install pyenv-win

(すべての環境変数もインストールされる)

インストールを完了させる

アプリ実行エイリアスの設定

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
(訳:Windows 10 1905 以降の場合は、「スタート > アプリ実行エイリアスの管理」で組み込みの Python ランチャーを無効にし、Pythonの「アプリインストーラー」エイリアスをオフにする必要がある場合がある。)

Windows 10 1905 以降の場合は、「スタート > 設定 > アプリ > アプリと機能 > アプリ実行エイリアス」でアプリインストーラー(python.exe と python3.exe)をオフにする

スクリーンショット (223).png

PYENV と PYENV_HOME を環境変数に追加

(Chocolatey を使用してインストールした場合はスキップ)

PowerShell または Windows 8 以降のターミナルで実行

[System.Environment]::SetEnvironmentVariable('PYENV',$env:USERPROFILE + "\.pyenv\pyenv-win\","User")
[System.Environment]::SetEnvironmentVariable('PYENV_HOME',$env:USERPROFILE + "\.pyenv\pyenv-win\","User")

PYENV_HOME は pipenv をサポートするためのもの

pyenv コマンドにアクセスできるようにする

(Chocolatey を使用してインストールした場合はスキップ)

PowerShell または Windows 8 以降のターミナルで実行

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

pyenv のバージョン確認

ターミナルアプリを閉じて再度開き、次のコマンドを実行

pyenv --version

command not found エラーが出た場合

対処法

command not found エラーが返ってきた場合 、すべての環境変数が正しく設定されていることを確認する。(設定で「環境変数を編集」と検索)

pyenv : このシステムではスクリプトの実行が無効になっているため... とエラーが出た場合

対処法

エラーの原因は実行ポリシーが初期値のままであること。(参考ページ

以下のコマンドを実行し、再試行する。

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process

実行ポリシーの変更
実行ポリシーは、信頼されていないスクリプトからの保護に役立ちます。実行ポリシーを変更すると、about_Execution_Policies のヘルプ トピック
(https://go.microsoft.com/fwlink/?LinkID=135170) で説明されているセキュリティ上の危険にさらされる可能性があります。実行ポリシーを変更しますか?
[Y] はい(Y)  [A] すべて続行(A)  [N] いいえ(N)  [L] すべて無視(L)  [S] 中断(S)  [?] ヘルプ (既定値は "N"): y

(私の実行環境の場合、Git Bash ではこのエラーは出ず、PowerShell ではこのエラーが出た)

Visual Studio Code またはその他のターミナルが組み込まれた IDE を使用している場合は、再起動・再試行する

Pyenv の shims を再構成

ホームディレクトリから次のコマンドを実行

pyenv rehash

No version installed. Please install one with 'pyenv install <version>'. とエラーが出た場合

対処法

pyenv install --list を実行して使用できるバージョンを確認し、pyenv install 3.9.6 のように python をインストールし、再試行する。

pyenv の実行

最後に pyenv と実行し、以下のように pyenv がサポートしているコマンドリストが出力されることを確認する。

pyenv 2.64.11
Usage: pyenv <command> [<args>]

Some useful pyenv commands are:
   commands     List all available pyenv commands
   duplicate    Creates a duplicate python environment
   local        Set or show the local application-specific Python version
   global       Set or show the global Python version
   shell        Set or show the shell-specific Python version
   install      Install a Python version using python-build
   uninstall    Uninstall a specific Python version
   update       Update the cached version DB
   rehash       Rehash pyenv shims (run this after installing executables)
   vname        Show the current Python version
   version      Show the current Python version and its origin
   version-name Show the current Python version
   versions     List all Python versions available to pyenv
   exec         Runs an executable by first preparing PATH so that the selected Python
   which        Display the full path to an executable
   whence       List all Python versions that contain the given executable

See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv-win/pyenv-win#readme

参考

53
42
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
53
42