IPython Alias to Preload Your Favorite Modules and Activate Autoreload

Grab your own powerful IPython with a very simple way

Yasufumi TANIGUCHI
Towards Data Science

--

TL;DR

You just copy this alias below and paste it to your .bashrc, .zshrc or some configure file:

alias ipy="ipython --no-confirm-exit --no-banner --quick --InteractiveShellApp.extensions=\"['autoreload']\" --InteractiveShellApp.exec_lines=\"['%autoreload 2', 'import os,sys']\""

I often use IPython to develop my library or do some research, because IPython has really great features as follows:

  • Run common shell commands: ls, cp, rm, etc.
  • Also, run any shell command with !(some command).
  • IPython provides a lot of magic commands: run, debug, timeit, etc
  • Great autocompletion
  • Preload your favorite modules in Python
  • Autoreload Extension

I think you’ve already known IPython provides a good Python interpreter, but also known that you need to prepare profileto customize IPython like preloading or activating an extension. I think this is kind of troublesome. So I explain the way to preload your favorite modules or activate autoreload without a configuration file in this post.

Preload your favorite modules in Python

You can execute Python codes like below when IPython startup. In this case, I’m preloading os and sys modules.

ipython --InteractiveShellApp.exec_lines="['import os,sys']"

If you’d like to preload PyTorch or Tensorflow, here is what you want:

ipython --InteractiveShellApp.exec_lines="['import torch', 'import tensorflow as tf']"

Activate the autoreload extension

autoreload reloads modules automatically before executing your code. You can activate autoreload as follows:

ipython --InteractiveShellApp.extensions="['autoreload']" --InteractiveShellApp.exec_lines="['%autoreload 2']"

By using --InteractiveShellApp.extensoins , you can load IPython extensions. To enable autoreload , you need to execute %autoreload 2.

Additional tips

You can remove the banner:

ipython --no-banner

Also, you can remove confirmation:

ipython --no-confirm-exit

Grab your own powerful IPython

I recommend you to combine all and make it an alias:

alias ipy="ipython --no-confirm-exit --no-banner --quick --InteractiveShellApp.extensions=\"['autoreload']\" --InteractiveShellApp.exec_lines=\"['%autoreload 2', 'import os,sys']\""

Note that --quick is an option to quick startup without config files. Now you can change this alias in your own favorite way easily. You might want to load additional modules like PyTorch , TensorFlow , Keras , scikit-learn and more.

That’s all for my post. In this post, I explained the way to customize your IPython with a very simple way and make it powerful. This post is for someone who doesn’t like set-up your own config files and wants a simpler way to do it. I hope my post will help you. If you’d like to customize IPython a lot more or use Jupyter, check this awesome post: How to Automatically Import Your Favorite Libraries into IPython or a Jupyter Notebook. Thank you for reading.

--

--