[Colaboratory / gym] !pip install gym==0.21.0 のエラーを回避

# 状況

- Google Colaboratory 上で `!pip install gym==0.21.0` を実行したところ、
    - 2024年2月中旬頃まではエラーなく実行できていた
    - 今日(2024/03/20)は、エラーが発生した


# 前提

- 後続処理で `pybullet_envs` を使う
- なぜかわからないけど、 gym==0.21.0 じゃないと `import pybullet_envs` に失敗する
- なので、なんとしても gym==0.21.0 をインストールしたい


# エラー発生状況

```python
!pip install gym==0.21.0

# 実行結果
Collecting gym==0.21.0
  Downloading gym-0.21.0.tar.gz (1.5 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.5/1.5 MB 7.1 MB/s eta 0:00:00
  error: subprocess-exited-with-error
  
  × python setup.py egg_info did not run successfully.
  │ exit code: 1
  ╰─> See above for output.
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
  Preparing metadata (setup.py) ... error
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.
```


# 対策

調べていたら、以下のような対策に出くわした。

https://github.com/MineDojo/MineDojo/issues/69

`!pip install setuptools==65.5.0 pip==21` を先に実行しておくといいらしい。

```python
!pip install setuptools==65.5.0 pip==21
!pip install gym==0.21.0
```

これを実行すると、以下のようになった。


```bash
# 実行結果
Collecting setuptools==65.5.0
  Downloading setuptools-65.5.0-py3-none-any.whl (1.2 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.2/1.2 MB 8.3 MB/s eta 0:00:00
Collecting pip==21
  Downloading pip-21.0-py3-none-any.whl (1.5 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.5/1.5 MB 49.1 MB/s eta 0:00:00
Installing collected packages: setuptools, pip
  Attempting uninstall: setuptools
    Found existing installation: setuptools 67.7.2
    Uninstalling setuptools-67.7.2:
      Successfully uninstalled setuptools-67.7.2
  Attempting uninstall: pip
    Found existing installation: pip 23.1.2
    Uninstalling pip-23.1.2:
      Successfully uninstalled pip-23.1.2
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
ipython 7.34.0 requires jedi>=0.16, which is not installed.
cvxpy 1.3.3 requires setuptools>65.5.1, but you have setuptools 65.5.0 which is incompatible.
pip-tools 6.13.0 requires pip>=22.2, but you have pip 21.0 which is incompatible.
Successfully installed pip-21.0 setuptools-65.5.0
WARNING: The following packages were previously imported in this runtime:
  [_distutils_hack,pkg_resources,setuptools]
You must restart the runtime in order to use newly installed versions.
Collecting gym==0.21.0
  Downloading gym-0.21.0.tar.gz (1.5 MB)
     |████████████████████████████████| 1.5 MB 2.6 MB/s 
Requirement already satisfied: numpy>=1.18.0 in /usr/local/lib/python3.10/dist-packages (from gym==0.21.0) (1.25.2)
Requirement already satisfied: cloudpickle>=1.2.0 in /usr/local/lib/python3.10/dist-packages (from gym==0.21.0) (2.2.1)
Building wheels for collected packages: gym
  Building wheel for gym (setup.py) ... error
  ERROR: Failed building wheel for gym
  Running setup.py clean for gym
Failed to build gym
Installing collected packages: gym
  Attempting uninstall: gym
    Found existing installation: gym 0.25.2
    Uninstalling gym-0.25.2:
      Successfully uninstalled gym-0.25.2
    Running setup.py install for gym ... done
  DEPRECATION: gym was installed using the legacy 'setup.py install' method, because a wheel could not be built for it. A possible replacement is to fix the wheel build issue reported above. You can find discussion regarding this at https://github.com/pypa/pip/issues/8368.
Successfully installed gym-0.21.0
```

1回目はこうなるので、
その後、以下のような RESTART SESSION ボタンを押して再起動し、

![スクリーンショット 2024-03-20 19.10.42.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/327407/6b2a4604-7a0b-06dc-8788-5bc2234b6e34.png)

もう一度最初の以下のセルから実行し直すと、

```python
!pip install setuptools==65.5.0 pip==21
!pip install gym==0.21.0
```

上記セルの実行が成功し、 pybullet_envs も import できるようになった。

```python
!pip install pybullet

import pybullet_envs
import pybullet as p
```

# 終わりに

しかし、なぜ成功したのかが全然わからない！！

理由はわからないけど、古い setuptools と pip じゃないと、 gym==0.21.0 の build に失敗するみたい... >_<

なぜか、 `setuptools 67.7.2` と `pip 23.1.2` から古いバージョンに切り替えると動くもんね。。