« [ game ] カードバトルのソーシャルゲームがつまらない理由 | トップページ | 2011年ノート PC 私的ランキング »

2012/01/06

[ python ] IPython ver0.12 でシェル起動時にモジュールをロードする方法

Abstract
  1. IPython は環境変数 PYTHONSTARTUP をみない
  2. IPython のバージョンアップで ipythonrc は廃止
  3. IPython 起動時にモジュールを import する方法

IPython は環境変数 PYTHONSTARTUP をみない

Python の使い勝手向上に大きく貢献してくれる IPython ですが、欠点の一つとして 環境変数 PYTHONSTARTUP を無視することが挙げられます。

これまでは、よく使う os や sys などのモジュールを起動時にインポートするには ipythonrc という設定ファイルの execfile セクションに PYTHONSTARTUP と同じファイルを指定していました。が・・・

IPython のバージョンアップで ipythonrc は廃止されました

IPython ver 0.11 以降は仕様が変更されてしまいました。
設定ファイル ipythonrc を書いて IPython を起動すると下記のように警告が表示されます。

$ ipython
WARNING: Found old IPython config file u'/home/fomalhaut/.ipython/ipythonrc' (modified by user)

  The IPython configuration system has changed as of 0.11, and these files will
  be ignored. See http://ipython.github.com/ipython-doc/dev/config for details
  of the new config system.
  To start configuring IPython, do `ipython profile create`, and edit
  `ipython_config.py` in /profile_default.
  If you need to leave the old config files in place for an older version of
  IPython and want to suppress this warning message, set
  `c.InteractiveShellApp.ignore_old_config=True` in the new config.

簡約すると「バージョン 0.11 で設定方式が変更された。これからは ipython_config.py で各種設定をしろ」ということです。

IPython 起動時にモジュールを import する方法

ipython_config.py の c.InteractiveShellApp.exec_files に PYTHONSTARTUP と同じファイルを指定します。

ipython_config.py は Linux の場合は $HOME/.ipython/profile_default ディレクトリ、 Windows の場合は %userprofile%\_ipython/profile_default フォルダの中にあります。

たとえば python_startup.py を実行したい場合は下記 1 行を追記 ( すでに c.InteractiveShellApp.exec_files が存在する場合は修正 ) します。

設定例

c.InteractiveShellApp.exec_files = [ "python_startup.py" ]

ファイルを保存したら IPython を起動して確認しましょう。

うまくいっているようです。

|

« [ game ] カードバトルのソーシャルゲームがつまらない理由 | トップページ | 2011年ノート PC 私的ランキング »

コメント

この記事へのコメントは終了しました。

トラックバック


この記事へのトラックバック一覧です: [ python ] IPython ver0.12 でシェル起動時にモジュールをロードする方法:

« [ game ] カードバトルのソーシャルゲームがつまらない理由 | トップページ | 2011年ノート PC 私的ランキング »