pyenv + pipenv環境でjupyter lab / notebookを使う

# まえがき
お久しぶりです．久しぶりすぎて書き方を忘れ始めていますがまえがきということで僕のいる学科では**ぴえん**（🥺）のことを**ぴえんぶ**（pyenv）といっている人がいたりいなかったり...笑

# 対象者
タイトルからわかる通り，pythonの使用環境を**pyenv + pipenv**で管理している人に向けて書きます．
まわり道というか自分が出くわしたことを含めて書いているのでやり方だけ見たい方は**最後だけ（やり方ってところを）見て**いただければすぐにわかります．

# jupyter lab / notebookを使う

## jupyter labとjupyter notebookの違い
jupyter labはjupyter notebookの後継機として様々な拡張機能を増やしたIDEとのこと．


## 自分が出くわしたこと
僕は今まで，この環境で

```Terminal
pipenv install jupyter
```
でインストールをしてからjupyter notebookを使用していました．
しかしなんとなく使いずらさを感じてjupyter labに移行しようと考えました．

そこでjupyter labの[github page](https://github.com/jupyterlab/jupyterlab)を訪れると

```Terminal
pipenv install jupyterlab
```
でいけるとのこと．python 3.8.2の環境で実行してみると

```Terminal
v install jupyterlab 
Installing jupyterlab…
Adding jupyterlab to Pipfile's [packages]…
✔ Installation Succeeded 
Pipfile.lock (3c2aeb) out of date, updating to (fca090)…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
Building requirements...
Resolving dependencies...
✘ Locking Failed! 
Traceback (most recent call last):
  File "/Users/hoge/.local/share/virtualenvs/hoge_HOGE/lib/python3.8/site-packages/qtpy/__init__.py", line 204, in <module>
    from PySide import __version__ as PYSIDE_VERSION  # analysis:ignore
ModuleNotFoundError: No module named 'PySide'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/Cellar/pipenv/2020.8.13_1/libexec/lib/python3.9/site-packages/pipenv/resolver.py", line 807, in <module>
    main()
  File "/usr/local/Cellar/pipenv/2020.8.13_1/libexec/lib/python3.9/site-packages/pipenv/resolver.py", line 802, in main
    _main(parsed.pre, parsed.clear, parsed.verbose, parsed.system, parsed.write,
  File "/usr/local/Cellar/pipenv/2020.8.13_1/libexec/lib/python3.9/site-packages/pipenv/resolver.py", line 785, in _main
    resolve_packages(pre, clear, verbose, system, write, requirements_dir, packages)
  File "/usr/local/Cellar/pipenv/2020.8.13_1/libexec/lib/python3.9/site-packages/pipenv/resolver.py", line 758, in resolve_packages
    results = clean_results(results, resolver, project)
  File "/usr/local/Cellar/pipenv/2020.8.13_1/libexec/lib/python3.9/site-packages/pipenv/resolver.py", line 640, in clean_results
    entry_dict = translate_markers(entry.get_cleaned_dict(keep_outdated=False))
  File "/usr/local/Cellar/pipenv/2020.8.13_1/libexec/lib/python3.9/site-packages/pipenv/resolver.py", line 239, in get_cleaned_dict
    self.validate_constraints()
  File "/usr/local/Cellar/pipenv/2020.8.13_1/libexec/lib/python3.9/site-packages/pipenv/resolver.py", line 565, in validate_constraints
    constraints = self.get_constraints()
  File "/usr/local/Cellar/pipenv/2020.8.13_1/libexec/lib/python3.9/site-packages/pipenv/resolver.py", line 493, in get_constraints
    pipfile_constraint = self.get_pipfile_constraint()
  File "/usr/local/Cellar/pipenv/2020.8.13_1/libexec/lib/python3.9/site-packages/pipenv/resolver.py", line 507, in get_pipfile_constraint
    return self.constraint_from_parent_conflicts()
  File "/usr/local/Cellar/pipenv/2020.8.13_1/libexec/lib/python3.9/site-packages/pipenv/resolver.py", line 527, in constraint_from_parent_conflicts
    if not p.requirements:
  File "/usr/local/Cellar/pipenv/2020.8.13_1/libexec/lib/python3.9/site-packages/pipenv/resolver.py", line 625, in __getattribute__
    return super(Entry, self).__getattribute__(key)
  File "/usr/local/Cellar/pipenv/2020.8.13_1/libexec/lib/python3.9/site-packages/pipenv/resolver.py", line 370, in requirements
    self.project.environment.get_package_requirements(self.name)
  File "/usr/local/Cellar/pipenv/2020.8.13_1/libexec/lib/python3.9/site-packages/pipenv/environment.py", line 708, in get_package_requirements
    return [self._get_requirements_for_package(p, key_tree) for p in nodes]
  File "/usr/local/Cellar/pipenv/2020.8.13_1/libexec/lib/python3.9/site-packages/pipenv/environment.py", line 708, in <listcomp>
    return [self._get_requirements_for_package(p, key_tree) for p in nodes]
  File "/usr/local/Cellar/pipenv/2020.8.13_1/libexec/lib/python3.9/site-packages/pipenv/environment.py", line 685, in _get_requirements_for_package
    d['dependencies'] = [
  File "/usr/local/Cellar/pipenv/2020.8.13_1/libexec/lib/python3.9/site-packages/pipenv/environment.py", line 686, in <listcomp>
    cls._get_requirements_for_package(c, key_tree, parent=node,
  File "/usr/local/Cellar/pipenv/2020.8.13_1/libexec/lib/python3.9/site-packages/pipenv/environment.py", line 677, in _get_requirements_for_package
    d = node.as_dict()
  File "/usr/local/Cellar/pipenv/2020.8.13_1/libexec/lib/python3.9/site-packages/pipenv/vendor/pipdeptree.py", line 284, in as_dict
    'installed_version': self.installed_version,
  File "/usr/local/Cellar/pipenv/2020.8.13_1/libexec/lib/python3.9/site-packages/pipenv/vendor/pipdeptree.py", line 251, in installed_version
    return guess_version(self.key, self.UNKNOWN_VERSION)
  File "/usr/local/Cellar/pipenv/2020.8.13_1/libexec/lib/python3.9/site-packages/pipenv/vendor/pipdeptree.py", line 123, in guess_version
    m = import_module(pkg_key)
  File "/Users/hoge/.pyenv/versions/3.8.2/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/hoge/.local/share/virtualenvs/hoge-HOGE/lib/python3.8/site-packages/qtpy/__init__.py", line 210, in <module>
    raise PythonQtError('No Qt bindings could be found')
qtpy.PythonQtError: No Qt bindings could be found
```

うわー．めっちゃエラー吐かれら．面倒だ．これが最初の感想．読む気も起こらず適当に調べてみると

## やり方

```Terminal
pipenv run jupyter lab
```
をやればいいとのこと．実行してみるとなんのエラーもなくjupyter labが起動してくれました．めでたし．さらに，jupyter notebookにおいても

```Terminal
pipenv run jupyter notebook
```
でいけた．わざわざインストールする必要がなかったらしい．**けど！**まだ**続き**があります．

## runの後に起きたこと
まだ乗り換えたばかりなので多くのことは書けませんが，上記のやり方の後，念のため

```Terminal
pipenv install jupyterlab
```
をしたところ先ほど出ていたエラーをみることなくインストールできちゃいました．この後に

```Terminal
pipenv run jupyter lab
jupyter lab
```
のどちらでも起動ができました．さらにここでなんとなく

```Terminal
pipenv uninstall jupyterlab
```
からの

```Terminal
pipenv run jupyter lab
jupyter lab
```
の両方を試したところ起動できなくなりました．後者のほうは起動できなくなるのはわかりますが，さっきまでできていた前者まで使えなくなるのはよくわかりませんでした．なのでjupyter labを再インストールして使用しています．これはjupyter notebookも同じ挙動をすると思います．

# まとめ
まだまだ知らないことだらけだ...早く機械学習ネタ書けるようになりたいな．