How can I recreate the effect of IPython's 

2019-04-01 01:46发布

问题:

I'd like to create a profile that does the same thing as IPython's --pylab flag "by hand". What should the contents of a script be — package imports, namespace designations, settings, etc. — to achieve this?

As an alternative, I'd also be interested in whether there is a way to check that the --pylab flag was set when the current IPython session was launched.


Simply doing from matplotlib.pylab import * doesn't work, nor does %pylab (since magics don't seem to be allowed in profiles).

回答1:

Digging into the help and man pages ($ ipython --help-all then followed by a search for "pylab") reveals:

--pylab=<CaselessStrEnum> (InteractiveShellApp.pylab) # mean is an alias for

--InteractiveShellApp.pylab=<CaselessStrEnum>
    Default: None
    Choices: ['auto', 'gtk', 'inline', 'osx', 'qt', 'qt4', 'tk', 'wx']
    Pre-load matplotlib and numpy for interactive use, selecting a particular
    matplotlib backend and loop integration.
--InteractiveShellApp.pylab_import_all=<Bool>
    Default: True
    If true, IPython will populate the user namespace with numpy, pylab, etc.
    and an ``import *`` is done from numpy and pylab, when using pylab mode.
    When False, pylab mode should not import any names into the user namespace.


So c.InteractiveShellApp.pylab='auto' in whatever configuration or profile you are loading will do the trick.

(Agreed with Andrew that hiding like this is unpythonic.)



回答2:

In an ipython sense, you can do this by creating a new iPython profile

ipython profile create <name>

Edit the ipython_config.py directory. On newer installs of ipython under linux, this file will be in ~/.config/ipython/profile_<name> directory, but you can find it if you're unsure with:

ipython profile locate <name>

For me, I can I edit the appropriate file with the bash command:

vim `ipython profile locate <name>`/ipython_config.py

Edit the appropriate variables in that file (see the ipython docs).


In a more general sense, you can force python to run arbitrary code at startup by setting the PYTHONSTARTUP environment variable, and pointing it at a python file with commands in it. To be equivalent to --pylab that file would need to have the following contents (equivalent to ipython 1.1).

# See: http://ipython.org/ipython-doc/rel-1.1.0/api/generated/IPython.core.magics.pylab.html
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 *

From past experience setting your PYTHONSTARTUP variable is probably a bad idea. All your Python code must have an x-session (or equivalent), unless you're setting a backend that doesn't pop up a plot window. It also pollutes the global namespace for everything you run and will slow down the interpreter (matplotlib in particular takes a noticeable amount of time to import on my machines).