LoginSignup
3
5

More than 5 years have passed since last update.

スタートアップ時にインポート中のモジュールを表示

Last updated at Posted at 2016-11-02

ご存知の通りホームディレクトリ上の
~\.ipython\profile_default\startup\startup.ipy
なるものにpythonコード入れておくと、IpythonやJupyter起動したときに自動的に実行してくれる。

その際、よく入れるコマンドとしてnumpyやpandasのインポートを行う。

startup.ipy(改良前)
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns  # <- matplotlibの強化版

しかしながらいくつもインポートしてると、インポートに時間がかかったりして今何をインポートしているのか見えづらいのが少し気にかかった。
そこで、importexecで実行してやって、インポート中のモジュールをprintするように改造した。

startup.ipy(改良後)
__modules__ = ('numpy',
           'pandas',
           'matplotlib.pyplot',
           'seaborn')
__standfor__ = {'numpy': 'np',
            'pandas': 'pd',
            'matplotlib.pyplot': 'plt',
            'seaborn': 'sns'}

for __module__ in __modules__:
    __execution__ = 'import %s as %s' % (__module__, __standfor__[__module__])
    print(__execution__)
    exec(__execution__)

こうすることで、Ipython立ち上げる時に、真っ暗画面の裏でpythonが何をやっているのかわからない状態によるストレスから解放されました。
おしまい。

更新2016/11/12
__hogehoge__のようにアンダーバー二つでくくると外部から見えなくなる。
影響ないと思うけど、後になって「指定した覚えのないmoduleとかstandforとかいう変数ナンダ???」ってならないようにする措置。

3
5
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
3
5