LoginSignup
2
2

More than 3 years have passed since last update.

Jupyter Notebook起動時に開く初期ディレクトリをうまく指定できなかったお話。

Last updated at Posted at 2019-07-31

Jupyter Notebook環境構築を行っていたとき、あることで1時間くらい時間を溶かしてしまったmacユーザーhogeさんのお話です。

1. Jupyter起動時に開くディレクトリを変更したい

Jupyter Notebookを起動したとき、デフォルトではユーザーのホームディレクトリ一覧が表示されます。なにかと不便なのでjupyterディレクトリを作成し、これをデフォルトのディレクトリにしたいとhogeさんは思いました。


$ /Users/hoge/ #デフォルト設定ではここだが…

$ /Users/hoge/Documents/jupyter #起動時にここを開くようにしたい

2. Jupyter Notebook設定ファイル(jupyter_notebook_config.py)の生成

「まずはTerminalでJupyter Notebook用の設定ファイルを作成しよう。生成されたかどうかちゃんとチェックも。」

$ jupyter notebook --generate-config #jupyter_notebook_config.pyの生成
$ ls -a ~ #隠しディレクトリを表示
. .. .vim .zshrc .jupyter …

$ cd .jupyter #.jupyterに入る
$ ls 
custom                        jupyter_notebook_config.py
jupyter_nbconvert_config.json migrated
jupyter_notebook_config.json  nbconfig

「できた。」
設定ファイルは、ホームディレクトリの隠しフォルダである~/.jupyter/に生成されました。

3. jupyter_notebook_config.py の中身をいじる

jupyter_notebook_config.pyの中身を見てみようと思ったhogeさんは、全然慣れていない(hjkl移動もしてない)けどなんとなくかっこいいと思っているvimでこれを開きました。

$ vi jupyter_notebook_config.py #viとvimは同じコマンドにしてある

# Configuration file for jupyter-notebook.

#------------------------------------------------------------------------------
# Application(SingletonConfigurable) configuration
#------------------------------------------------------------------------------

## This is an application.

## The date format used by logging formatters for %(asctime)s
#c.Application.log_datefmt = '%Y-%m-%d %H:%M:%S'#c.NotebookApp.notebook_dir = ' ' #261行目のこれを編集する

ディレクトリを変更するためにはどうやら261行目のコメントアウトを消して、c.NotebookApp.notebook_dirに好きなパスを指定してあげればいいとインターネットにいる人たちが書いた記事の多くには書いてあったから以下のように書いたそうです。

c.NotebookApp.notebook_dir = '~/Documents/jupyter'

しかし…

4. 「パスを指定してもうまくいかない!」

「jupyterを起動したら開けないんだが?」


$ jupyter notebook

[C 18:07:55.548 NotebookApp] Bad config encountered during initialization:
[C 18:07:55.548 NotebookApp] No such notebook dir: ''/Users/hoge/.jupyter/~/Documents/jupyter'' 

「なんで.jupyter直下から~が始まっちゃうの?そんな読み方しないでほしい。じゃあ~記号を使わないで書けばちゃんと読み取って許してくれるのかな?」

c.NotebookApp.notebook_dir = '/Users/hoge/Documents/jupyter' #書き直しました

$ jupyter notebook #ちゃんと起動しました

起動しましたね。ちなみに無いとは思いますがUsersの前の/を忘れるとこんなことになります。


c.NotebookApp.notebook_dir = 'Users/hoge/Documents/jupyter'

$ jupyter notebook 

[C 18:16:30.026 NotebookApp] Bad config encountered during initialization:
[C 18:16:30.026 NotebookApp] No such notebook dir: ''/Users/hoge/.jupyter/Users/hoge/Documents/jupyter''
2
2
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
2
2