Steve Barnes
1 min readApr 18, 2020

--

On windows machines the home dir defaults to C:\Users\username or can be found with the `set %USERPROFILE%` command.

The problems are:

  • that I have with this method of working is that you can end up with a significant delay in every ipython use (while all of these modules load) on my machine loading just numpy, pandas & matplotlib takes 2.36 seconds and I often don’t use/need them.
  • If I am in an ipython session and %save then try to use the resulting script from python it will not work without editing.
  • Code sharing will only work if I remember to take an additional manual step to add the necessary imports in.
  • The standard mantra: “Explicit is better than implicit”

On searching for the possibility of having a default cell in Notebooks I found this Stack Overflow item where Dan Goldner suggests keeping a firstcell.py file to hand and using:

%load firstcell.py

to get it’s contents as the initial contents. His example firstcell.py was:

%reload_ext autoreload
%autoreload 2

import numpy as np
import pandas as pd
...

I like this as I can have multiple template import scripts, e.g. vis_imp.ipy for visualisation. I found that I didn’t need the first two lines which makes things clearer.

When I run the magic %load it does my standard imports for the task that I am doing but if I am in a notebook and save the notebook others can use it and if I am in ipython I can just use %save filename.py 1- to get a file that others can use, (or I can use from python).

--

--

Steve Barnes
Steve Barnes

Written by Steve Barnes

0 Followers

Residing in Wales but living in my head. FOSS & Python fan & advocate.

No responses yet