IPython notebook is a very nice experimentation platform, however it seems to be a little unintuitive to use when using as part of a larger ‘non-experimental’ codebase. The following shows how a couple of directory tweaks can be made without altering any IPython configuration files.

If there’s a better way to do this, please let me know. This feels like a hack.

Subdirectory model desired

When coding up a larger project, it’s helpful to have everything in the familiar directory structure :

./                         # {BASE DIRECTORY}
./src/Module1/__init__.py  # ...(etc)
./src/Module2/__init__.py  # ...(etc)

And standard scripts (for instance src/xyz.py) can be run in {BASE} by simply :

python src/xyz.py

Such scripts can import the internal modules straightforwardly (eg: import Module1), and the base directory for accessing the data will be data/.

At the same time, the IPython notebooks are kept in a separate notebooks/ directory, which is what messes up all the paths.

IPython notebook preamble

Open up a new IPython notebook in notebooks/, and have the following cell at the start to pull in the modules, and data with the correct relative paths :

%cd ../src
import Module1
%cd ..
p = Module1.Obj('Something', 'datafile', 17)

Running matplotlib thereafter :

%matplotlib inline
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = (16.0, 8.0)
import numpy as np

Martin Andrews

{Finance, Software, AI} entrepreneur, living in Singapore with my family.

blog comments powered by Disqus