[Translation] Automatic import of libraries in IPython and Jupyter Notebook

[Translation] Automatic import of libraries in IPython and Jupyter Notebook


Hello!

Today we will examine a very short, but useful life hack about what needs to be done so as not to introduce, for example, “import pandas as pd” 10 times a day.

Also let's not forget, and automate and reduce everything effectively:

image

For our life hacking you need:

  1. Go to ~/.ipython/profile_default;
  2. Create a folder named startup if it is not already there;
  3. Add a new Python file named start.py;
  4. Place your favorite import into this file;
  5. Launch IPython or Jupyter Notebook, and your favorite libraries will be automatically loaded every time!

For clarity, let's visualize everything. First, the location of start.py:

image

Here is the content of my start.py file:

  import pandas as pd
 import numpy as np

 # Pandas options
 pd.options.display.max_columns = 30
 pd.options.display.max_rows = 20

 from IPython import get_ipython
 ipython = get_ipython ()

 # If in ipython, load autoreload extension
 if 'ipython' in globals ():
  print ('\ nWelcome to IPython!')
  ipython.magic ('load_ext autoreload')
  ipython.magic ('autoreload 2')

 # Display all cell outputs in notebook
 from IPython.core.interactiveshell import InteractiveShell
 InteractiveShell.ast_node_interactivity = 'all'

 # Visualization
 import plotly.plotly as py
 import plotly.graph_objs as go
 from plotly.offline import iplot, init_notebook_mode
 init_notebook_mode (connected = True)
 import cufflinks as cf
 cf.go_offline (connected = True)
 cf.set_config_file (theme = 'pearl')

 print ('Your favorite library have been loaded.')
  

Now, when I start an IPython session, I see:

image

Also, we can confirm that the libraries were loaded by checking globals ():

  globals () ['pd']
 & lt; module 'pandas' from'/usr/local/lib/python3.6/site-packages/pandas/__init__.py'>
 globals () ['np']
 & lt; module 'numpy' from '/usr/local/lib/python3.6/site-packages/numpy/__init__.py'>
  

That's all. Now we can use our interactive session without having to enter commands to load these libraries!

Note


  • The file can be named as you like (start.py is easy to remember), and you can also have several startup/files. They run in lexicographical order when you start IPython.
  • If you do this in Jupyter, you won’t get a cell with import data, so if you are sharing Jupyter, be sure to copy the contents of start.py into the first cell. This will allow people to find out which libraries you are using.
  • If you are working on multiple computers, you will have to repeat the steps. Make sure you use the same start.py script to get the same import!
  • Thanks to a reply from Stack Overflow and official documentation .

If someone has something to add or share life hacking - write in the comments.

All knowledge!
Subscribe to the Neuron channel in the Telegram (@neurondata) - there are fresh articles and news from the world of data science appear every week. Thanks to everyone who helps and shares useful links, especially Vitaly Oreshkin and Andrey Bondarenko.

Source text: [Translation] Automatic import of libraries in IPython and Jupyter Notebook