LoginSignup
23
11

More than 3 years have passed since last update.

Jupyter Notebook on WSL がブラウザで自動起動しない場合の対応方法

Posted at

諸事情で Mac から Windows に回帰し (2 回目)、WSL で Mac と同じように jupyter notebook を実行したところ、ブラウザで自動起動しない事象に遭遇したので、その対応方法を紹介します。
改めて紹介するほどのものでもないかもしれませんが、エラーメッセージでググっても日本語の情報が見つからなかったので、ここにさらしておきます。

動作確認した環境

  • Windows 10 x64, Version 1909
  • Ubuntu 18.04 LTS on WSL1
  • Python 3.6.9
  • Jupyter Notebook 6.0.3

事象

pip install で jupyter をインストール後、jupyter notebook を実行すると、以下のようなエラーが表示されブラウザで自動起動しません。

$ jupyter notebook
[I 01:39:20.094 NotebookApp] Serving notebooks from local directory: /mnt/c/workspace
[I 01:39:20.094 NotebookApp] The Jupyter Notebook is running at:
[I 01:39:20.094 NotebookApp] http://localhost:8888/?token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
[I 01:39:20.095 NotebookApp]  or http://127.0.0.1:8888/?token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
[I 01:39:20.095 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 01:39:20.116 NotebookApp]

    To access the notebook, open this file in a browser:
        file:///home/wsluser/.local/share/jupyter/runtime/nbserver-xxxx-open.html
    Or copy and paste one of these URLs:
        http://localhost:8888/?token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
     or http://127.0.0.1:8888/?token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Start : This command cannot be run due to the error: The system cannot find the file specified.
At line:1 char:1
+ Start "file:///home/wsluser/.local/share/jupyter/runtime/nbserver-xx ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

ブラウザ (Windows) 側から WSL 上の nbserver-xxxx-open.html のパスを参照できないために発生するようですが、http://localhost:8888/?token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx の URL に飛ぶと起動します。

対応方法 #1

--NotebookApp.use_redirect_file=False を指定して実行します。

$ jupyter notebook --NotebookApp.use_redirect_file=False

毎回オプションを指定するのは面倒なので、適当に alias を設定して実行します。

$ echo "alias jn='jupyter notebook --NotebookApp.use_redirect_file=False'" >> ~/.bashrc
$ source ~/.bashrc
$ jn

対応方法 #2

--generate-config を指定して実行し、生成された config ファイル内で c.NotebookApp.use_redirect_file = False を設定します。その後、オプションなしで実行します。

// config を生成
$ jupyter notebook --generate-config
Writing default config to: /home/wsluser/.jupyter/jupyter_notebook_config.py

// デフォルトの設定を確認
$ grep use_redirect_file ~/.jupyter/jupyter_notebook_config.py
#c.NotebookApp.use_redirect_file = True

// コメントアウトを外し、False に変更
$ sed -i -e 's/#c.NotebookApp.use_redirect_file = True/c.NotebookApp.use_redirect_file = False/g' ~/.jupyter/jupyter_notebook_config.py

// 変更後の設定を確認
$ grep use_redirect_file ~/.jupyter/jupyter_notebook_config.py
c.NotebookApp.use_redirect_file = False

// オプションなしで実行
$ jupyter notebook

参考

23
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
23
11