Python Pyenv for Windows OS 安裝與設定步驟


  1. 說明
    1. 安裝步驟
      1. 使用 pip 安裝 pyenv
      2. 設定環境變數
      3. 驗證安裝成功
    2. 使用方式
      1. version | 目前環境所使用的 python 版本
      2. versions | 檢視所有可選用的 python 版本
      3. install -l | 檢視可以安裝版本的 python
      4. install | 安裝特定版本的 python
      5. local | 設定特定專案使用特定版本 python
      6. global | 設定全域環境所使用特定版本 python
      7. rehash | 更新 python 環境資訊

教學如何在 Windows 作業系統安裝 pyenv,藉由 pyenv 可以在作業系統中管理全域或者專案中,分別要使用的 python 版本。同時也說明 pyenv 常用的指令包含 global, local, install, versions 等,只要五分鐘就能夠快速上手 pyenv。

logo

說明

pyenv-win | GitHub

安裝步驟

使用 pip 安裝 pyenv

可以使用 PowerShell:

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

或者 cmd:

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

設定環境變數

使用 PowerShell 來設定環境變數:

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

驗證安裝成功

首先關閉作用中的 terminal ( PowerShell & cmd ),重新開啟 terminal。

確認 pyenv 安裝的版本:

pyenv --version
# pyenv 2.64.9 

確認 pyenv 相關指令的使用:

pyenv

#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

使用方式

version | 目前環境所使用的 python 版本

pyenv version
# 3.10.0b3 (set by .\.python-version)
# pyenv global 3.7.2

versions | 檢視所有可選用的 python 版本

pyenv versions

install -l | 檢視可以安裝版本的 python

pyenv install -l

install | 安裝特定版本的 python

pyenv install 3.10.0b3

local | 設定特定專案使用特定版本 python

pyenv local 3.10.0b3

global | 設定全域環境所使用特定版本 python

pyenv global 3.9.6

rehash | 更新 python 環境資訊

如果有使用 pip 安裝或解安裝 library 以及異動特定 version Python 的資料夾內容,必須使用 rehash 通知 pyenv 重新對應相關的使用連結。

pyenv rehash