memo: pyenv: pip: command not foundになってしまった

# タイトルの通りの状況から復帰した
またやりそうなので、メモを残す

# 状況
```bash
$ pip list
You are using pip version 19.0.1, however version 19.0.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
```

upgradeしろと言われ、それに従うも。。

```bash
$ pip install --upgrade pip
Collecting pip
  Downloading https://files.pythonhosted.org/packages/d7/41/34dd96bd33958e52cb4da2f1bf0818e396514fd4f4725a79199564cd0c20/pip-19.0.2-py2.py3-none-any.whl (1.4MB)
    100% |████████████████████████████████| 1.4MB 54kB/s 
Installing collected packages: pip
  Found existing installation: pip 19.0.1
    Uninstalling pip-19.0.1:
Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: 'RECORD'
Consider using the `--user` option or check the permissions.

You are using pip version 19.0.1, however version 19.0.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
```

あれ、何かうまくいかないです。。
--userって目立っているし、あ、これはsudo使えってフラグかな、という思い込みで

```bash
$ sudo pip install --upgrade pip
Password:
pyenv: pip: command not found

The `pip' command exists in these Python versions:
  3.5.3
```

##pipが動かなく
うぬぬ、これ以降pipコマンド様がいうことを聞いてくれませんでした。
sudoなしでも同じ状況

```bash
$ pip install --upgrade pip
pyenv: pip: command not found

The `pip' command exists in these Python versions:
  3.5.3
```
3.5.3なら使えるということで、以下を試す。

```
$ pyenv versions
  system
  3.5.3
* 3.6.0 (set by /Users/obara/.pyenv/version)
$ pyenv local 3.5.3
$ pip install --upgrade pip
Cache entry deserialization failed, entry ignored
Collecting pip
  Cache entry deserialization failed, entry ignored
  Downloading https://files.pythonhosted.org/packages/d7/41/34dd96bd33958e52cb4da2f1bf0818e396514fd4f4725a79199564cd0c20/pip-19.0.2-py2.py3-none-any.whl (1.4MB)
    100% |████████████████████████████████| 1.4MB 89kB/s 
Installing collected packages: pip
  Found existing installation: pip 9.0.1
    Uninstalling pip-9.0.1:
      Successfully uninstalled pip-9.0.1
Successfully installed pip-19.0.2

```
すんなりできるが、普段3.6使ってるしな。。戻っても状況変わらず。

```bash
$ pyenv local 3.6.0
$ pip install --upgrade pip
pyenv: pip: command not found

The `pip' command exists in these Python versions:
  3.5.3
```

##再インストールを試みる

```bash
$ python -m pip install -U pip
Collecting pip
  Using cached https://files.pythonhosted.org/packages/d7/41/34dd96bd33958e52cb4da2f1bf0818e396514fd4f4725a79199564cd0c20/pip-19.0.2-py2.py3-none-any.whl
Error checking for conflicts.
Traceback (most recent call last):
  File "/Users/home/.pyenv/versions/3.6.0/Python.framework/Versions/3.6/lib/python3.6/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2584, in version
    return self._version
  File "/Users/home/.pyenv/versions/3.6.0/Python.framework/Versions/3.6/lib/python3.6/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2691, in __getattr__
    raise AttributeError(attr)
AttributeError: _version

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/home/.pyenv/versions/3.6.0/Python.framework/Versions/3.6/lib/python3.6/site-packages/pip/_internal/commands/install.py", line 503, in _warn_about_conflicts
    package_set, _dep_info = check_install_conflicts(to_install)
  File "/Users/home/.pyenv/versions/3.6.0/Python.framework/Versions/3.6/lib/python3.6/site-packages/pip/_internal/operations/check.py", line 108, in check_install_conflicts
    package_set, _ = create_package_set_from_installed()
  File "/Users/home/.pyenv/versions/3.6.0/Python.framework/Versions/3.6/lib/python3.6/site-packages/pip/_internal/operations/check.py", line 47, in create_package_set_from_installed
    package_set[name] = PackageDetails(dist.version, dist.requires())
  File "/Users/home/.pyenv/versions/3.6.0/Python.framework/Versions/3.6/lib/python3.6/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2589, in version
    raise ValueError(tmpl % self.PKG_INFO, self)
ValueError: ("Missing 'Version:' header and/or METADATA file", Unknown [unknown version] (/Users/home/.pyenv/versions/3.6.0/Python.framework/Versions/3.6/lib/python3.6/site-packages))
Installing collected packages: pip
  Found existing installation: pip 19.0.1
    Uninstalling pip-19.0.1:
Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: 'RECORD'
Consider using the `--user` option or check the permissions.

You are using pip version 19.0.1, however version 19.0.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
```

##うぬぬ、ということで、何かゴミが残っていると考え、動かない状態だし、一度削除しよう。

```bash

$ sudo python -m pip uninstall pip
The directory '/Users/home/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Uninstalling pip-19.0.1:
  Would remove:
    /Users/home/.pyenv/versions/3.6.0/Python.framework/Versions/3.6/lib/python3.6/site-packages/pip-19.0.1.dist-info/*
    /Users/home/.pyenv/versions/3.6.0/Python.framework/Versions/3.6/lib/python3.6/site-packages/pip/*
Proceed (y/n)? y
  Successfully uninstalled pip-19.0.1
```

#解決

##そして再インストール

```bash

$ python -m pip install -U pip
Cache entry deserialization failed, entry ignored
Collecting pip
  Using cached https://files.pythonhosted.org/packages/d7/41/34dd96bd33958e52cb4da2f1bf0818e396514fd4f4725a79199564cd0c20/pip-19.0.2-py2.py3-none-any.whl
Installing collected packages: pip
  Found existing installation: pip 9.0.1
    Uninstalling pip-9.0.1:
      Successfully uninstalled pip-9.0.1
Successfully installed pip-19.0.2
```

やったー٩( ･ิω･ิ )و 

##安心したのもつかの間

listが取れていなかった

```bash
$ pip list

...
ValueError: ("Missing 'Version:' header and/or METADATA file", Unknown [unknown version] (/Users/obara/.pyenv/versions/3.6.0/Python.framework/Versions/3.6/lib/python3.6/site-packages))
```
ググってみると、以下のようなissuesが。
https://github.com/pypa/pip/issues/6177

## 同じように再インストールを試みる

```
$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 1662k  100 1662k    0     0  31852      0  0:00:53  0:00:53 --:--:-- 53892
MB:python$ python get-pip.py
Collecting pip
  Using cached https://files.pythonhosted.org/packages/d7/41/34dd96bd33958e52cb4da2f1bf0818e396514fd4f4725a79199564cd0c20/pip-19.0.2-py2.py3-none-any.whl
/var/folders/b5/6b7hmsds3dlc703bpsx5tw480000gn/T/tmpbi8emds2/pip.zip/pip/_internal/req/req_install.py:391: UserWarning: Unbuilt egg for Unknown [unknown version] (/Users/home/.pyenv/versions/3.6.0/Python.framework/Versions/3.6/lib/python3.6/site-packages)
Collecting wheel
  Downloading https://files.pythonhosted.org/packages/7c/d7/20bd3c501f53fdb0b7387e75c03bd1fce748a1c3dd342fc53744e28e3de1/wheel-0.33.0-py2.py3-none-any.whl
Error checking for conflicts.
Traceback (most recent call last):
  File "/var/folders/b5/6b7hmsds3dlc703bpsx5tw480000gn/T/tmpbi8emds2/pip.zip/pip/_vendor/pkg_resources/__init__.py", line 2897, in _dep_map
    return self.__dep_map
  File "/var/folders/b5/6b7hmsds3dlc703bpsx5tw480000gn/T/tmpbi8emds2/pip.zip/pip/_vendor/pkg_resources/__init__.py", line 2691, in __getattr__
    raise AttributeError(attr)
AttributeError: _DistInfoDistribution__dep_map

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/var/folders/b5/6b7hmsds3dlc703bpsx5tw480000gn/T/tmpbi8emds2/pip.zip/pip/_vendor/pkg_resources/__init__.py", line 2888, in _parsed_pkg_info
    return self._pkg_info
  File "/var/folders/b5/6b7hmsds3dlc703bpsx5tw480000gn/T/tmpbi8emds2/pip.zip/pip/_vendor/pkg_resources/__init__.py", line 2691, in __getattr__
    raise AttributeError(attr)
AttributeError: _pkg_info

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/var/folders/b5/6b7hmsds3dlc703bpsx5tw480000gn/T/tmpbi8emds2/pip.zip/pip/_internal/commands/install.py", line 503, in _warn_about_conflicts
    package_set, _dep_info = check_install_conflicts(to_install)
  File "/var/folders/b5/6b7hmsds3dlc703bpsx5tw480000gn/T/tmpbi8emds2/pip.zip/pip/_internal/operations/check.py", line 108, in check_install_conflicts
    package_set, _ = create_package_set_from_installed()
  File "/var/folders/b5/6b7hmsds3dlc703bpsx5tw480000gn/T/tmpbi8emds2/pip.zip/pip/_internal/operations/check.py", line 47, in create_package_set_from_installed
    package_set[name] = PackageDetails(dist.version, dist.requires())
  File "/var/folders/b5/6b7hmsds3dlc703bpsx5tw480000gn/T/tmpbi8emds2/pip.zip/pip/_vendor/pkg_resources/__init__.py", line 2635, in requires
    dm = self._dep_map
  File "/var/folders/b5/6b7hmsds3dlc703bpsx5tw480000gn/T/tmpbi8emds2/pip.zip/pip/_vendor/pkg_resources/__init__.py", line 2899, in _dep_map
    self.__dep_map = self._compute_dependencies()
  File "/var/folders/b5/6b7hmsds3dlc703bpsx5tw480000gn/T/tmpbi8emds2/pip.zip/pip/_vendor/pkg_resources/__init__.py", line 2908, in _compute_dependencies
    for req in self._parsed_pkg_info.get_all('Requires-Dist') or []:
  File "/var/folders/b5/6b7hmsds3dlc703bpsx5tw480000gn/T/tmpbi8emds2/pip.zip/pip/_vendor/pkg_resources/__init__.py", line 2890, in _parsed_pkg_info
    metadata = self.get_metadata(self.PKG_INFO)
  File "/var/folders/b5/6b7hmsds3dlc703bpsx5tw480000gn/T/tmpbi8emds2/pip.zip/pip/_vendor/pkg_resources/__init__.py", line 1410, in get_metadata
    value = self._get(self._fn(self.egg_info, name))
  File "/var/folders/b5/6b7hmsds3dlc703bpsx5tw480000gn/T/tmpbi8emds2/pip.zip/pip/_vendor/pkg_resources/__init__.py", line 1522, in _get
    with open(path, 'rb') as stream:
FileNotFoundError: [Errno 2] No such file or directory: '/Users/home/.pyenv/versions/3.6.0/Python.framework/Versions/3.6/lib/python3.6/site-packages/~ip-19.0.1.dist-info/METADATA'
Installing collected packages: pip, wheel
  Found existing installation: pip 19.0.2
    Uninstalling pip-19.0.2:
      Successfully uninstalled pip-19.0.2
Successfully installed pip-19.0.2 wheel-0.33.0
```

なんか変なエラーが出ているもののとりあえずインストールはできたが。。

```bash

$ pip list
Exception:
Traceback (most recent call last):
  File "/Users/home/.pyenv/versions/3.6.0/Python.framework/Versions/3.6/lib/python3.6/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2584, in version
    return self._version
  File "/Users/home/.pyenv/versions/3.6.0/Python.framework/Versions/3.6/lib/python3.6/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2691, in __getattr__
    raise AttributeError(attr)
AttributeError: _version

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/home/.pyenv/versions/3.6.0/Python.framework/Versions/3.6/lib/python3.6/site-packages/pip/_internal/cli/base_command.py", line 179, in main
    status = self.run(options, args)
  File "/Users/home/.pyenv/versions/3.6.0/Python.framework/Versions/3.6/lib/python3.6/site-packages/pip/_internal/commands/list.py", line 148, in run
    self.output_package_listing(packages, options)
  File "/Users/home/.pyenv/versions/3.6.0/Python.framework/Versions/3.6/lib/python3.6/site-packages/pip/_internal/commands/list.py", line 205, in output_package_listing
    data, header = format_for_columns(packages, options)
  File "/Users/home/.pyenv/versions/3.6.0/Python.framework/Versions/3.6/lib/python3.6/site-packages/pip/_internal/commands/list.py", line 271, in format_for_columns
    row = [proj.project_name, proj.version]
  File "/Users/home/.pyenv/versions/3.6.0/Python.framework/Versions/3.6/lib/python3.6/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2589, in version
    raise ValueError(tmpl % self.PKG_INFO, self)
ValueError: ("Missing 'Version:' header and/or METADATA file", Unknown [unknown version] (/Users/home/.pyenv/versions/3.6.0/Python.framework/Versions/3.6/lib/python3.6/site-packages))
```

変わってない:sob:泣ける

## もう少し探る

pip管理のライブラリを確認してみる

```bash
$ python
Python 3.6.0 (default, Jan 19 2018, 13:33:51) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
>>> df = pd.DataFrame()
>>> print(df)
Empty DataFrame
Columns: []
Index: []
>>> 

```

とりあえずは問題なく動いているっぽい。
とりあえず、メッセージに出ているディレクトリを覗いてみる:rolling_eyes:

```
$ ls ~/.pyenv/versions/3.6.0/Python.framework/Versions/3.6/lib/python3.6/site-packages
-ip-19.0.1.dist-info/
...
wheel-0.33.0.dist-info/
~ip-19.0.1.dist-info/
```
なにやら怪しいディレクトリが。。
pipを再度アンインストールしても上記ディレクトリは残っているので、思い切って過去バージョンの遺物と思われるディレクトリを削除して再インストールを試みる。

```bash
$ sudo python -m pip uninstall pip
Password:
The directory '/Users/obara/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Uninstalling pip-19.0.2:
  Would remove:
    /Users/obara/.pyenv/versions/3.6.0/Python.framework/Versions/3.6/bin/pip
    /Users/obara/.pyenv/versions/3.6.0/Python.framework/Versions/3.6/bin/pip3
    /Users/obara/.pyenv/versions/3.6.0/Python.framework/Versions/3.6/bin/pip3.6
    /Users/obara/.pyenv/versions/3.6.0/Python.framework/Versions/3.6/lib/python3.6/site-packages/pip-19.0.2.dist-info/*
    /Users/obara/.pyenv/versions/3.6.0/Python.framework/Versions/3.6/lib/python3.6/site-packages/pip/*
Proceed (y/n)? y   
  Successfully uninstalled pip-19.0.2
MB:python$ pip
pyenv: pip: command not found

The `pip' command exists in these Python versions:
  3.5.3

$ rm -rf ~/.pyenv/versions/3.6.0/Python.framework/Versions/3.6/lib/python3.6/site-packages/~ip-19.0.1.dist-info
$ rm -rf ~/.pyenv/versions/3.6.0/Python.framework/Versions/3.6/lib/python3.6/site-packages/-ip-19.0.1.dist-info

$ python get-pip.py
Collecting pip
  Using cached https://files.pythonhosted.org/packages/d7/41/34dd96bd33958e52cb4da2f1bf0818e396514fd4f4725a79199564cd0c20/pip-19.0.2-py2.py3-none-any.whl
Installing collected packages: pip
Successfully installed pip-19.0.2
```

```bash

$ pip list
Package                  Version  
------------------------ ---------
astroid                  2.0.4    

...

wheel                    0.33.0   
wrapt                    1.10.11  
```

やったー٩( ･ิω･ิ )و （再