macのマルチユーザ環境でjupyter notebookが500エラーを返す場合の「おまじない」


(追記)
pipenvをuserインストールしてなかっただけ

```
pip3 install --user pipenv

pipenv install jupyterlab

pipenv run jupyter lab
```

2021年7月21日 本当にできた。
権限変更だけでは対応されない。
pipenvを使う必要がありそう.





以下は使わない。

## この記事は？
macbookで環境構築をしているとたまに遭遇するjupyter notebookの`500`エラーの解決方法.

原因は、マルチユーザを使用している場合の権限問題。

結論は、以下の`おまじない`で権限付与することで解消する。

マルチユーザ環境で環境構築する場合は以下のコマンドを事前に通しておくのが基本(らしい)

```
> sudo chown -R $(whoami):admin /usr/local/*
Password:
> sudo chmod -R g+w /usr/local/*
```

参考まで

## 遭遇していたエラー

jupyter notebookの画面は`500`エラー

```bash

% pipenv run jupyter notebook
[I 09:56:04.985 NotebookApp] [jupyter_nbextensions_configurator] enabled 0.4.1
[I 09:56:04.986 NotebookApp] ローカルディレクトリからノートブックをサーブ: /Users/nohara/Code/210219_genmodel/nohara
[I 09:56:04.986 NotebookApp] Jupyter Notebook 6.2.0 is running at:
[I 09:56:04.986 NotebookApp] http://localhost:8888/
[I 09:56:04.986 NotebookApp] サーバを停止し全てのカーネルをシャットダウンするには Control-C を使って下さい(確認をスキップするには2回)。
[E 09:56:10.011 NotebookApp] Uncaught exception GET /notebooks/notebooks/001.ipynb (::1)
    HTTPServerRequest(protocol='http', host='localhost:8888', method='GET', uri='/notebooks/notebooks/001.ipynb', version='HTTP/1.1', remote_ip='::1')
    Traceback (most recent call last):
      File "/Users/nohara/.local/share/virtualenvs/nohara-wTv0ObQ1/lib/python3.7/site-packages/tornado/web.py", line 1704, in _execute
        result = await result
      File "/Users/nohara/.local/share/virtualenvs/nohara-wTv0ObQ1/lib/python3.7/site-packages/tornado/gen.py", line 775, in run
        yielded = self.gen.send(value)
      File "/Users/nohara/.local/share/virtualenvs/nohara-wTv0ObQ1/lib/python3.7/site-packages/notebook/notebook/handlers.py", line 101, in get
        get_frontend_exporters=get_frontend_exporters
      File "/Users/nohara/.local/share/virtualenvs/nohara-wTv0ObQ1/lib/python3.7/site-packages/notebook/base/handlers.py", line 516, in render_template
        return template.render(**ns)
      File "/Users/nohara/.local/share/virtualenvs/nohara-wTv0ObQ1/lib/python3.7/site-packages/jinja2/environment.py", line 1090, in render
        self.environment.handle_exception()
      File "/Users/nohara/.local/share/virtualenvs/nohara-wTv0ObQ1/lib/python3.7/site-packages/jinja2/environment.py", line 832, in handle_exception
        reraise(*rewrite_traceback_stack(source=source))
      File "/Users/nohara/.local/share/virtualenvs/nohara-wTv0ObQ1/lib/python3.7/site-packages/jinja2/_compat.py", line 28, in reraise
        raise value.with_traceback(tb)
      File "/Users/nohara/.local/share/virtualenvs/nohara-wTv0ObQ1/lib/python3.7/site-packages/notebook/templates/notebook.html", line 1, in top-level template code
        {% extends "page.html" %}
      File "/Users/nohara/.local/share/virtualenvs/nohara-wTv0ObQ1/lib/python3.7/site-packages/notebook/templates/page.html", line 154, in top-level template code
        {% block header %}
      File "/Users/nohara/.local/share/virtualenvs/nohara-wTv0ObQ1/lib/python3.7/site-packages/notebook/templates/notebook.html", line 115, in block "header"
        {% for exporter in get_frontend_exporters() %}
      File "/Users/nohara/.local/share/virtualenvs/nohara-wTv0ObQ1/lib/python3.7/site-packages/notebook/notebook/handlers.py", line 40, in get_frontend_exporters
        for name in get_export_names():
      File "/Users/nohara/.local/share/virtualenvs/nohara-wTv0ObQ1/lib/python3.7/site-packages/nbconvert/exporters/base.py", line 141, in get_export_names
        e = get_exporter(exporter_name)(config=config)
      File "/Users/nohara/.local/share/virtualenvs/nohara-wTv0ObQ1/lib/python3.7/site-packages/nbconvert/exporters/base.py", line 102, in get_exporter
        if getattr(exporter(config=config), 'enabled', True):
      File "/Users/nohara/.local/share/virtualenvs/nohara-wTv0ObQ1/lib/python3.7/site-packages/nbconvert/exporters/templateexporter.py", line 325, in __init__
        super().__init__(config=config, **kw)
      File "/Users/nohara/.local/share/virtualenvs/nohara-wTv0ObQ1/lib/python3.7/site-packages/nbconvert/exporters/exporter.py", line 114, in __init__
        self._init_preprocessors()
      File "/Users/nohara/.local/share/virtualenvs/nohara-wTv0ObQ1/lib/python3.7/site-packages/nbconvert/exporters/templateexporter.py", line 491, in _init_preprocessors
        conf = self._get_conf()
      File "/Users/nohara/.local/share/virtualenvs/nohara-wTv0ObQ1/lib/python3.7/site-packages/nbconvert/exporters/templateexporter.py", line 509, in _get_conf
        if conf_path.exists():
      File "/usr/local/var/pyenv/versions/3.7.9/lib/python3.7/pathlib.py", line 1361, in exists
        self.stat()
      File "/usr/local/var/pyenv/versions/3.7.9/lib/python3.7/pathlib.py", line 1183, in stat
        return self._accessor.stat(self)
    PermissionError: [Errno 13] Permission denied: '/usr/local/share/jupyter/nbconvert/templates/conf.json'
[E 09:56:10.025 NotebookApp] {
      "Host": "localhost:8888",
      "Connection": "keep-alive",
      "Upgrade-Insecure-Requests": "1",
      "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36",
      "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
      "Sec-Fetch-Site": "same-origin",
      "Sec-Fetch-Mode": "navigate",
      "Sec-Fetch-User": "?1",
      "Sec-Fetch-Dest": "document",
      "Referer": "http://localhost:8888/tree/notebooks",
      "Accept-Encoding": "gzip, deflate, br",
      "Accept-Language": "ja,en-US;q=0.9,en;q=0.8",
      "Cookie": "_xsrf=2|711944f6|7f8c3cce96a88612c14f2db934d1c5be|1613696109; username-localhost-8888=\"2|1:0|10:1613696113|23:username-localhost-8888|44:OWExMGI0NjMyOGI0NDllYThhMTI0ZWI3OTViNjQ5MTY=|5b9cdad129f61551a9e52a02f9654cc559e610cc241c813086ef80db800c1c79\""
    }
[E 09:56:10.025 NotebookApp] 500 GET /notebooks/notebooks/001.ipynb (::1) 143.510000ms referer=http://localhost:8888/tree/notebooks


```

`nbconvert`配下のファイル操作で`PermissionError`が出ている。

```
    PermissionError: [Errno 13] Permission denied: '/usr/local/share/jupyter/nbconvert/templates/conf.json'
```

試しにディレクトリに入ろうとすると権限エラー

```
> cd /usr/local/share/jupyter/nbconvert/templates/
cd: permission denied: /usr/local/share/jupyter/nbconvert/templates/
```

### 解決方法

マルチユーザの場合は以下の`おまじない`が必要

```
> sudo chown -R $(whoami):admin /usr/local/*
Password:
> sudo chmod -R g+w /usr/local/*

```

無事開けました。