Previously, explanations for Using IPython's rich features and How to use IPython Notebook 72d744554f0454139c0f), Start IPython notebook server I wrote the method.
This time, I will continue to write about switching profiles, customizing prompts, and using the interactive debugger (pdb). Interactive debuggers are especially important, and if you can master them, your development productivity will increase dramatically.
The default profile can be generated with the following command:
ipython profile create default
This will create a directory like .ipython / profile_default in the location set as your home directory. This default part is the name of the profile.
You can mainly set the following items in the profile.
You can specify the profile name by specifying options such as ipython --profile = default when starting IPython. More information on profiles can be found in the Documentation (http://ipython.org/ipython-doc/2/config/intro.html#profiles).
profile_default / ipython_config.py defines the overall settings for the IPython shell prompt. You can read more about this file in the IPython documentation (http://ipython.org/ipython-doc/2/development/config.html#python-configuration-files).
In this file, an instance is first created as c = get_config (), and the environment is set by setting the value of this instance. For example, in my environment, the prompt is as follows.
c.PromptManager.out_template = r'[\#]: '
c.PromptManager.in2_template = r'{color.Green}[{color.LightGreen}\D{color.Green}]: '
c.PromptManager.in_template = r'{color.Yellow}\h{color.LightBlue}[{color.LightCyan}\T{color.LightBlue}]{color.Green}[{color.LightGreen}\#{color.Green}]: '
out defines the output line, in2 defines the continuation line, and in defines the input line.
In the configuration file, the c instance is used for operation, but the% config magic command is used to operate the environment from the shell.
You can also refer to Documentation for prompt extension.
In the above example, some libraries are imported by the initialization script as written earlier.
Commands that start with% are called magic commands. If you type only% and enter a tab, IPython's tab completion function will display the list. For example,% pwd will display the current directory. % who displays the namespace of a valid instance.
hoge = "fuga"
%who
#=> hoge
Also, commands starting with! Are passed to the shell as shell commands.
In IPython, the extension of pdb Debugger is available. The% debug command debugs the last exception that occurred. You can set the% pdb command to automatically start the debugger when an exception occurs.
The IPython documentation (http://ipython.org/ipython-doc/2/interactive/reference.html#using-the-python-debugger-pdb) also describes how to use pdb.
If you define the set_trace () and debug () functions as follows, you can write set_trace () anywhere in your code to stop execution and start the debugger.
def set_trace():
from IPython.core.debugger import Pdb
Pdb(color_scheme='Linux').set_trace(sys._getframe().f_back)
def debug(f, *args, **kwargs):
from IPython.core.debugger import Pdb
pdb = Pdb(color_scheme='Linux')
return pdb.runcall(f, *args, **kwargs)
You can also use the debug () function to launch an interactive debugger for any function. For example, the example below launches an interactive debugger for the function f.
For example, in this state, pressing a will display a list of the arguments of the current function, pressing w will display the stack trace at the current position, pressing s will step in, and u / d will raise or lower the function call stack. I will. You can also display variables, add arbitrary operations, and so on.
Such an interactive environment is so convenient and productive that once you learn to develop in IPython, you can't let go of it without it. It's no exaggeration to say that IPython and your favorite editor are enough for developing in Python.
An introduction to data analysis with Python http://www.oreilly.co.jp/books/9784873116556/
Recommended Posts