: Command-line use
: IPython An enhanced Interactive
: Initial configuration...
Quick tips
IPython can be used as an improved replacement for the Python prompt, and for that
you don't really need to read any more of this manual. But in this section we'll
try to summarize a few tips on how to make the most effective use of it for everyday
Python development, highlighting things you might miss in the rest of the manual
(which is getting long). We'll give references to parts in the manual which provide
more detail when appropriate.
- The TAB key. TAB-completion, especially for attributes, is a convenient way to
explore the structure of any object you're dealing with. Simply type object_name.<TAB>
and a list of the object's attributes will be printed (see sec. 6.5
for more). Tab completion also works on file and directory names, which combined
with IPython's alias system allows you to do from within IPython many of the things
you normally would need the system shell for.
- Explore your objects. Typing object_name? will print all sorts of details
about any object, including docstrings, function definition lines (for call arguments)
and constructor details for classes. The magic commands %pdoc, %pdef,
%psource and %pfile will respectively print the docstring,
function definition line, full source code and the complete file for any object
(when they can be found). If automagic is on (it is by default), you don't need
to type the '%' explicitly. See sec. 6.4 for more.
- The %run magic command allows you to run any python script and load all
of its data directly into the interactive namespace. Since the file is re-read
from disk each time, changes you make to it are reflected immediately (in contrast
to the behavior of import). I rarely use import for code I am
testing, relying on %run instead. See sec. 6.2 for more
on this and other magic commands, or type the name of any magic command and ? to
get details on it. See also sec. 6.9 for a recursive reload command.
%run also has special flags for timing the execution of your scripts
(-t) and for executing them under the control of either Python's pdb
debugger (-d) or profiler (-p). With all of these, %run
can be used as the main tool for efficient interactive development of code which
you write in your editor of choice.
- Use the Python debugger, pdb2. The %pdb command allows you to toggle on and off the automatic invocation
of the pdb debugger at any uncaught exception. The advantage of this is that pdb
starts inside the function where the exception occurred, with all data still
available. You can print variables, see code, execute statements and even walk
up and down the call stack to track down the true source of the problem (which
often is many layers in the stack above where the exception gets triggered).
Running programs with %run and pdb active can be an efficient to develop
and debug code, in many cases eliminating the need for print statements
or external debugging tools. I often simply put a 1/0 in a place where
I want to take a look so that pdb gets called, quickly view whatever variables
I need to or test various pieces of code and then remove the 1/0.
Note also that `%run -d' activates pdb and automatically sets
initial breakpoints for you to step through your code, watch variables, etc. See
Sec. 6.12 for details.
- Use the output cache. All output results are automatically stored in a global dictionary
named Out and variables named _1, _2, etc. alias them.
For example, the result of input line 4 is available either as Out[4]
or as _4. Additionally, three variables named _, __
and ___ are always kept updated with the for the last three results.
This allows you to recall any previous result and further use it for new calculations.
See Sec. 6.12 for more.
- Put a ';' at the end of a line to supress the printing of output. This
is useful when doing calculations which generate long output you are not interested
in seeing. The _* variables ant the Out[] list do get
updated with the contents of the output, even if it is not printed. You can thus
still access the generated results this way for further processing.
- A similar system exists for caching input. All input is stored in a global list
called In , so you can re-execute lines 22 through 28 plus line 34 by
typing 'exec In[22:29]+In[34]' (using Python slicing notation).
If you need to execute the same set of lines often, you can assign them to a macro
with the %macro function. See sec. 6.11 for
more.
- Use your input history. The %hist command can show you all previous input,
without line numbers if desired (option -n) so you can directly copy and
paste code either back in IPython or in a text editor. You can also save all your
history by turning on logging via %logstart; these logs can later be
either reloaded as IPython sessions or used as code for your programs.
- Define your own macros with %macro. This can be useful for automating
sequences of expressions when working interactively.
- Define your own system aliases. Even though IPython gives you access to your system
shell via the ! prefix, it is convenient to have aliases to the system
commands you use most often. This allows you to work seamlessly from inside IPython
with the same commands you are used to in your system shell.
IPython comes with some pre-defined aliases and a complete system for changing
directories, both via a stack (see %pushd, %popd and %ds)
and via direct %cd. The latter keeps a history of visited directories
and allows you to go to any previously visited one.
- Use Python to manipulate the results of system commands. The `!!' special
syntax, and the %sc and %sx magic commands allow you to capture
system output into Python variables.
- Expand python variables when calling the shell (either via `!' and `!!'
or via aliases) by prepending a $ in front of them. You can also expand
complete python expressions. See sec. 6.7 for more.
- Use profiles to maintain different configurations (modules to load, function definitions,
option settings) for particular tasks. You can then have customized versions of
IPython for specific purposes. See sec. 7.3 for more.
- Embed IPython in your programs. A few lines of code are enough to load a complete
IPython inside your own programs, giving you the ability to work with your data
interactively after automatic processing has been completed. See sec. 9
for more.
- Use the Python profiler. When dealing with performance issues, the %run
command with a -p option allows you to run complete programs under the
control of the Python profiler. The %prun command does a similar job
for single Python expressions (like function calls).
- Use %edit to have almost multiline editing. While IPython doesn't support
true multiline editing, this command allows you to call an editor on the spot,
and IPython will execute the code you type in there as if it were typed interactively.
If you have your own favorite tip on using IPython efficiently for a certain task
(especially things which can't be done in the normal Python interpreter), don't
hesitate to send it!
: Command-line use
: IPython An enhanced Interactive
: Initial configuration...
Fernando Perez
Ê¿À®16ǯ12·î13Æü
::...
免责声明:
当前网页内容, 由
大妈 ZoomQuiet
使用工具:
ScrapBook :: Firefox Extension
人工从互联网中收集并分享;
内容版权归原作者所有;
本人对内容的有效性/合法性不承担任何强制性责任.
若有不妥, 欢迎评注提醒:
或是邮件反馈可也:
askdama[AT]googlegroups.com
点击注册~> 获得 100$ 体验券:
订阅 substack 体验古早写作:
关注公众号, 持续获得相关各种嗯哼:
自怼圈/年度番新
关于 ~ DebugUself with DAMA ;-)
粤ICP备18025058号-1
公安备案号:
44049002000656
...::