[PYTHON] Show modules being imported at startup

As you know, on your home directory ~\.ipython\profile_default\startup\startup.ipy If you put the python code in the stuff, it will be executed automatically when Ipython or Jupyter is started.

At that time, import numpy and pandas as commands that are often inserted.

startup.ipy(Before improvement)


import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns  # <-Enhanced version of matplotlib

However, when I imported several, I was a little worried that it would take a long time to import and it would be difficult to see what I was importing. So, I ran ʻimport with ʻexec and modified it to print the module being imported.

startup.ipy(After improvement)


__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__)

This freed me from the stress of not knowing what python was doing behind a pitch black screen when launching Ipython. That's it.

Update 2016/11/12 If you enclose it with two underscores like __hogehoge__, it will not be visible from the outside. I don't think it will affect you, but it's a measure to prevent you from becoming "a variable Nanda such as a module or standfor that you don't remember specifying ???" later.

Recommended Posts

Show modules being imported at startup
Runlevel at OS startup