python3 開発環境構築 on WSL

## はじめに

- 気づいたらすでに python も python3 もインストールされていましたが pyenv で python をインストールして使うように設定してみました
- 以下をインストールしてパスを通します
  - pyenv
  - pipenv
  - python3
- ※すでに rails な開発環境は導入済なので以下の手順の通り実施しても何かしらライブラリ不足があるかもしれません

## 環境

- Windows10
- ubuntu 18.04 on WSL
- fish

## install pyenv

- git からインストール
  - https://github.com/pyenv/pyenv

```bash

$ git clone https://github.com/pyenv/pyenv.git ~/.pyenv
$ echo 'set PATH $HOME/.pyenv/bin $PATH' >>  ~/.config/fish/config.fish
$ echo 'eval (pyenv init - | source)' >>  ~/.config/fish/config.fish
$ eval (pyenv init - | source)
$ which pyenv
/home/yourname/.pyenv/bin/pyenv
```

## install python3.7.0

- インストールできるバージョンを確認

```bash

$ pyenv install --list
  ...
  3.6.6
  3.7.0
  3.7-dev
  3.8-dev
  ...
```

- 現在時点での最新の3.7.0 をインストールしてみる

```bash

$ pyenv install -v 3.7.0
```

- しばらく待った後以下のエラーで失敗

```bash

Traceback (most recent call last):
  File "/tmp/python-build.20180910235918.12705/Python-3.7.0/Lib/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/tmp/python-build.20180910235918.12705/Python-3.7.0/Lib/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/tmp/python-build.20180910235918.12705/Python-3.7.0/Lib/ensurepip/__main__.py", line 5, in <module>
    sys.exit(ensurepip._main())
  File "/tmp/python-build.20180910235918.12705/Python-3.7.0/Lib/ensurepip/__init__.py", line 204, in _main
    default_pip=args.default_pip,
  File "/tmp/python-build.20180910235918.12705/Python-3.7.0/Lib/ensurepip/__init__.py", line 117, in _bootstrap
    return _run_pip(args + [p[0] for p in _PROJECTS], additional_paths)
  File "/tmp/python-build.20180910235918.12705/Python-3.7.0/Lib/ensurepip/__init__.py", line 27, in _run_pip
    import pip._internal
  File "/tmp/tmpirqvyjxt/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/__init__.py", line 42, in <module>
  File "/tmp/tmpirqvyjxt/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/cmdoptions.py", line 16, in <module>
  File "/tmp/tmpirqvyjxt/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/index.py", line 25, in <module>
  File "/tmp/tmpirqvyjxt/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/download.py", line 39, in <module>
  File "/tmp/tmpirqvyjxt/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/utils/glibc.py", line 3, in <module>
  File "/tmp/python-build.20180910235918.12705/Python-3.7.0/Lib/ctypes/__init__.py", line 7, in <module>
    from _ctypes import Union, Structure, Array
ModuleNotFoundError: No module named '_ctypes'
Makefile:1122: recipe for target 'install' failed
make: *** [install] Error 1

BUILD FAILED (Ubuntu 18.04 using python-build 1.2.7-3-g5603eb51)
```

- モジュールが足りないのでインストールする

```bash

$ sudo apt install libffi-dev
```

- リトライ

```bash

$ pyenv install -v 3.7.0

...

Installing collected packages: setuptools, pip
Successfully installed pip-10.0.1 setuptools-39.0.1
/tmp/python-build.20180911001219.25285 ~
~
Installed Python-3.7.0 to /home/ospear/.pyenv/versions/3.7.0
```

- インストール成功

```
$ which python
/home/yourname/.pyenv/shims/python
$ pyenv global 3.7.0
$ python --version
Python 3.7.0
```

## install pipenv

```bash

$ which pip
/home/yourname/.pyenv/shims/pip
$ pip --version
pip 10.0.1 from /home/yourname/.pyenv/versions/3.7.0/lib/python3.7/site-packages/pip (python 3.7)
$ pip install --upgrade pip
$ pip --version
pip 18.0 from /home/yourname/.pyenv/versions/3.7.0/lib/python3.7/site-packages/pip (python 3.7)
$ pip install pipenv
$ which pipenv
/home/yourname/.pyenv/shims/pipenv
$ pipenv --version
pipenv, version 2018.7.1
```

## 参考

- https://qiita.com/Nimimal/items/72c2e2a6a566d1f5d2a2
- https://qiita.com/sl2/items/1e503952b9506a0539ea