# pyenv

## 概要

* Python自身のバージョンを切り替えるためのもの。

## インストール

### Windows

pipが必要なので、まずはPythonをインストールする。

* <https://www.python.org/>

pipでpyenv-winをインストールする。

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

````

インストールされるのは以下となる。

```/C/User/{ユーザ―名}\.pyenv\pyenv-win```

環境変数を追加する。(管理者としてPowerShellを起動)

```powershell
[System.Environment]::SetEnvironmentVariable('PYENV',$env:USERPROFILE + "\.pyenv\pyenv-win\","User")
[System.Environment]::SetEnvironmentVariable('PATH', $HOME + "\.pyenv\pyenv-win\bin;" + $HOME + "\.pyenv\pyenv-win\shims;" + $env:Path,"Machine")
[System.Environment]::SetEnvironmentVariable('PYENV_ROOT',$env:USERPROFILE + "\.pyenv\pyenv-win\","User")
[System.Environment]::SetEnvironmentVariable('PYENV_HOME',$env:USERPROFILE + "\.pyenv\pyenv-win\","User")
````

### Macの場合

インストール

```shell
brew install pyenv
brew install pyenv-virtualenv
```

環境変数追加

```shell
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
```

### WSL

pyenvの取得

```shell
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
cd ~/.pyenv && src/configure && make -C src # optional
```

以下を入力

```shell
# the sed invocation inserts the lines at the start of the file
# after any initial comment lines
sed -Ei -e '/^([^#]|$)/ {a \
export PYENV_ROOT="$HOME/.pyenv"
a \
export PATH="$PYENV_ROOT/bin:$PATH"
a \
' -e ':a' -e '$!{n;ba};}' ~/.profile
echo 'eval "$(pyenv init --path)"' >>~/.profile

echo 'eval "$(pyenv init -)"' >> ~/.bashrc
```

ここで一旦terminalを再起動。

依存パッケージをインストール。

```shell
sudo apt install -y pip libbz2-dev libssl-dev libreadline-dev libsqlite3-dev libffi-dev
```

## 使用方法

インストール可能なバージョン一覧の確認

```shell
pyenv install --list
```

インストール

```shell
pyenv install 3.8.10
```

現在のバージョン確認

```shell
pyenv version
```

インストール済みのバージョン一覧確認

```shell
pyenv versions
```

全体で利用するバージョンの設定

```shell
pyenv global 3.8.10
```

特定のディレクトリ配下のみ利用するバージョンの設定

```shell
cd PROJECT_DIR
pyenv local 3.9.1
pyenv version # 確認
```

## pyenv自体のアップデート

pyenv-updateを使用する

* [pyenv のアップデート方法](https://zenn.dev/utah/articles/6b4c5cec60c45b)

```powershell
git clone https://github.com/pyenv/pyenv-update.git ($env:PYENV_ROOT + "plugins\pyenv-update")
pyenv update
```

## 参考

* [Windows 10 で Python のインストールから Poetry と pyenv の利用](https://qiita.com/kerobot/items/3f4064d5174676080585)
* [pyenv 利用のまとめ](https://qiita.com/m3y/items/45c7be319e401b24fca8)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://nakamura-shogo.gitbook.io/dev-wiki/python/huan-jing-gou-zhu/python_pyenv.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
