Enthought Training on Demand: pylab mode vs. explicit imports

Overview

As you work through the Enthought Training on Demand course materials, you may notice slight discrepancies between the code shown in some of the lecture videos and corresponding code in lecture notebooks and exercises. You may even encounter a traceback when you try to follow along with some videos if you start typing into an empty Jupyter notebook instead of using the lecture notes associated with the video.

This article explains the reasons for the discrepancies and what to do about them. (See related article Modules are already available in Canopy's Python (PyLab) prompt, but not in a script).

Interactive Python and pylab Mode

Python is used for different purposes, including software engineering (writing commercial applications like Canopy, for example) and data analysis. People who use Python for data analysis tend to start out by exploring their data interactively at the command line, and they commonly need a certain set of tools to do this - particularly numpy and matplotlib.  

What you'd normally do is to import the needed packages like this:

import numpy as np
import matplotlib.pyplot as plt

And then to call a function from one of these imported packages you would prepend the abbreviated name of the package to the function call, like this:

a = np.arange(10)

As a convenience for the interactive data analysis work flow, Canopy's IPython notebooks used to launch by default in what is called "pylab mode." When launched in pylab mode, IPython makes the following imports at startup:

import numpy
import matplotlib
from matplotlib import pylab, mlab, pyplot
np = numpy
plt = pyplot

from IPython.display import display
from IPython.core.pylabtools import figsize, getfigs

from pylab import *
from numpy import *

On the plus side, pylab mode allows you to drop the package name from many common function calls, like so:

a = arange(10) # Notice no np. before the call to arange() this time

This means writing a bit less code and fewer details to remember.

But it turns out that this caused a couple of problems as well. First, it confuses a lot of people who are new to the language.  The main reason is because when you write a script in an editor, pylab mode isn't in effect there and you need to do the imports and package naming explicitly. As a result, code that works fine at the pylab-enabled command line will give you errors when pasted into a script (as in the Canopy editor) and run from there.

Second, pylab mode loads all of the function names from numpy and matplotlib into the global namespace, which can cause name conflicts.  For example, if two packages loaded into the global namespace have a function called "sum()", one will silently replace the other and this can lead to subtle bugs. 

For these and other reasons, the Canopy developers decided to change the behavior of IPython notebooks (now called Jupyter notebooks) to conform to the ever-evolving Jupyter project. Now pylab mode is not enabled by default, and the community is recommending against its use in general.  The preferred method now is to make the following explicit imports at the start of your interactive session:

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt

And then you should prepend the package name in front of function calls like so:

a = np.arange(10)
plt.plot(a)

You could also import the individual functions that you use frequently directly into your global namespace, like this:

from matplotlib.pyplot import plot, figure, clf
from numpy import array, arange

This will import the specified functions (plot, figure, clf, array, and arange) into your global namespace and then you can call them directly without using the package name explicitly:

a = arange(10) # Note we dropped the np. before arange()
plot(a) # Note we dropped the plt. before plot()

Working along with the Enthought Training on Demand video lectures

The reason the code shown in some video lectures won't run as shown if you type it into an empty Jupyter notebook is because the lectures were recorded when pylab mode was still the default for IPython (Jupyter) notebooks.

If you want to type along with the code examples in the video lectures, you have three options:

  1. Follow along using the lecture notebook associated with the lecture video - the code there has been updated with explicit imports
  2. Add the recommended imports above (%matplotlib inline, etc.) and then prepend the package name to each function that needs it (this is the recommended approach, but requires more work on your part - good for learning, though)
  3. Add the magic command "%pylab inline" to the top of the notebook (it's just %pylab in an IPython command shell) and that will activate pylab mode in the notebook. This is only recommended as a convenience for the training - the official recommendation is not to use pylab mode any more for the reasons cited above.

For more information, see the lecture called Importing NumPy Libraries Into Python in the NumPy Enthought Training on Demand course (subscription required).

Have more questions? Submit a request

Comments

  • Avatar
    Jean-Paul Davis

    The suggestion above to use

    %matplotlib inline
    import numpy as np
    import matplotlib.pyplot as plt

    in a jupyter notebook does not work for me, using ipython 5.1.0, jupyter 4.2.1 on RHEL 6 linux in Firefox. Nothing happens when a plot should appear. Using the deprecated %pylab inline magic command does work.

Powered by Zendesk