##// END OF EJS Templates
Merge pull request #4607 from takluyver/tutorial-config-link...
Min RK -
r13716:4c7b96d8 merge
parent child Browse files
Show More
@@ -1,201 +1,201
1 1 .. _tutorial:
2 2
3 3 ======================
4 4 Introducing IPython
5 5 ======================
6 6
7 7 You don't need to know anything beyond Python to start using IPython – just type
8 8 commands as you would at the standard Python prompt. But IPython can do much
9 9 more than the standard prompt. Some key features are described here. For more
10 10 information, check the :ref:`tips page <tips>`, or look at examples in the
11 11 `IPython cookbook <https://github.com/ipython/ipython/wiki/Cookbook%3A-Index>`_.
12 12
13 13 If you've never used Python before, you might want to look at `the official
14 14 tutorial <http://docs.python.org/tutorial/>`_ or an alternative, `Dive into
15 15 Python <http://diveintopython.net/toc/index.html>`_.
16 16
17 17 The four most helpful commands
18 18 ===============================
19 19
20 20 The four most helpful commands, as well as their brief description, is shown
21 21 to you in a banner, every time you start IPython:
22 22
23 23 ========== =========================================================
24 24 command description
25 25 ========== =========================================================
26 26 ? Introduction and overview of IPython's features.
27 27 %quickref Quick reference.
28 28 help Python's own help system.
29 29 object? Details about 'object', use 'object??' for extra details.
30 30 ========== =========================================================
31 31
32 32 Tab completion
33 33 ==============
34 34
35 35 Tab completion, especially for attributes, is a convenient way to explore the
36 36 structure of any object you're dealing with. Simply type ``object_name.<TAB>``
37 37 to view the object's attributes (see :ref:`the readline section <readline>` for
38 38 more). Besides Python objects and keywords, tab completion also works on file
39 39 and directory names.
40 40
41 41 Exploring your objects
42 42 ======================
43 43
44 44 Typing ``object_name?`` will print all sorts of details about any object,
45 45 including docstrings, function definition lines (for call arguments) and
46 46 constructor details for classes. To get specific information on an object, you
47 47 can use the magic commands ``%pdoc``, ``%pdef``, ``%psource`` and ``%pfile``
48 48
49 49 .. _magics_explained:
50 50
51 51 Magic functions
52 52 ===============
53 53
54 54 IPython has a set of predefined 'magic functions' that you can call with a
55 55 command line style syntax. There are two kinds of magics, line-oriented and
56 56 cell-oriented. **Line magics** are prefixed with the ``%`` character and work much
57 57 like OS command-line calls: they get as an argument the rest of the line, where
58 58 arguments are passed without parentheses or quotes. **Cell magics** are
59 59 prefixed with a double ``%%``, and they are functions that get as an argument
60 60 not only the rest of the line, but also the lines below it in a separate
61 61 argument.
62 62
63 63 The following examples show how to call the builtin ``timeit`` magic, both in
64 64 line and cell mode::
65 65
66 66 In [1]: %timeit range(1000)
67 67 100000 loops, best of 3: 7.76 us per loop
68 68
69 69 In [2]: %%timeit x = range(10000)
70 70 ...: max(x)
71 71 ...:
72 72 1000 loops, best of 3: 223 us per loop
73 73
74 74 The builtin magics include:
75 75
76 76 - Functions that work with code: ``%run``, ``%edit``, ``%save``, ``%macro``,
77 77 ``%recall``, etc.
78 78 - Functions which affect the shell: ``%colors``, ``%xmode``, ``%autoindent``,
79 79 ``%automagic``, etc.
80 80 - Other functions such as ``%reset``, ``%timeit``, ``%%file``, ``%load``, or
81 81 ``%paste``.
82 82
83 83 You can always call them using the ``%`` prefix, and if you're calling a line
84 84 magic on a line by itself, you can omit even that::
85 85
86 86 run thescript.py
87 87
88 88 You can toggle this behavior by running the ``%automagic`` magic. Cell magics
89 89 must always have the ``%%`` prefix.
90 90
91 91 A more detailed explanation of the magic system can be obtained by calling
92 92 ``%magic``, and for more details on any magic function, call ``%somemagic?`` to
93 93 read its docstring. To see all the available magic functions, call
94 94 ``%lsmagic``.
95 95
96 96 .. seealso::
97 97
98 98 `Cell magics`_ example notebook
99 99
100 100 Running and Editing
101 101 -------------------
102 102
103 103 The ``%run`` magic command allows you to run any python script and load all of
104 104 its data directly into the interactive namespace. Since the file is re-read
105 105 from disk each time, changes you make to it are reflected immediately (unlike
106 106 imported modules, which have to be specifically reloaded). IPython also
107 107 includes :ref:`dreload <dreload>`, a recursive reload function.
108 108
109 109 ``%run`` has special flags for timing the execution of your scripts (-t), or
110 110 for running them under the control of either Python's pdb debugger (-d) or
111 111 profiler (-p).
112 112
113 113 The ``%edit`` command gives a reasonable approximation of multiline editing,
114 114 by invoking your favorite editor on the spot. IPython will execute the
115 115 code you type in there as if it were typed interactively.
116 116
117 117 Debugging
118 118 ---------
119 119
120 120 After an exception occurs, you can call ``%debug`` to jump into the Python
121 121 debugger (pdb) and examine the problem. Alternatively, if you call ``%pdb``,
122 122 IPython will automatically start the debugger on any uncaught exception. You can
123 123 print variables, see code, execute statements and even walk up and down the
124 124 call stack to track down the true source of the problem. This can be an efficient
125 125 way to develop and debug code, in many cases eliminating the need for print
126 126 statements or external debugging tools.
127 127
128 128 You can also step through a program from the beginning by calling
129 129 ``%run -d theprogram.py``.
130 130
131 131 History
132 132 =======
133 133
134 134 IPython stores both the commands you enter, and the results it produces. You
135 135 can easily go through previous commands with the up- and down-arrow keys, or
136 136 access your history in more sophisticated ways.
137 137
138 138 Input and output history are kept in variables called ``In`` and ``Out``, keyed
139 139 by the prompt numbers, e.g. ``In[4]``. The last three objects in output history
140 140 are also kept in variables named ``_``, ``__`` and ``___``.
141 141
142 142 You can use the ``%history`` magic function to examine past input and output.
143 143 Input history from previous sessions is saved in a database, and IPython can be
144 144 configured to save output history.
145 145
146 146 Several other magic functions can use your input history, including ``%edit``,
147 147 ``%rerun``, ``%recall``, ``%macro``, ``%save`` and ``%pastebin``. You can use a
148 148 standard format to refer to lines::
149 149
150 150 %pastebin 3 18-20 ~1/1-5
151 151
152 152 This will take line 3 and lines 18 to 20 from the current session, and lines
153 153 1-5 from the previous session.
154 154
155 155 System shell commands
156 156 =====================
157 157
158 158 To run any command at the system shell, simply prefix it with !, e.g.::
159 159
160 160 !ping www.bbc.co.uk
161 161
162 162 You can capture the output into a Python list, e.g.: ``files = !ls``. To pass
163 163 the values of Python variables or expressions to system commands, prefix them
164 164 with $: ``!grep -rF $pattern ipython/*``. See :ref:`our shell section
165 165 <system_shell_access>` for more details.
166 166
167 167 Define your own system aliases
168 168 ------------------------------
169 169
170 170 It's convenient to have aliases to the system commands you use most often.
171 171 This allows you to work seamlessly from inside IPython with the same commands
172 172 you are used to in your system shell. IPython comes with some pre-defined
173 173 aliases and a complete system for changing directories, both via a stack (see
174 174 %pushd, %popd and %dhist) and via direct %cd. The latter keeps a history of
175 175 visited directories and allows you to go to any previously visited one.
176 176
177 177
178 178 Configuration
179 179 =============
180 180
181 Much of IPython can be tweaked through :ref:`configuration <config_overview>`.
181 Much of IPython can be tweaked through :doc:`configuration </config/intro>`.
182 182 To get started, use the command ``ipython profile create`` to produce the
183 183 default config files. These will be placed in
184 184 :file:`~/.ipython/profile_default`, and contain comments explaining
185 185 what the various options do.
186 186
187 187 Profiles allow you to use IPython for different tasks, keeping separate config
188 188 files and history for each one. More details in :ref:`the profiles section
189 189 <profiles>`.
190 190
191 191 Startup Files
192 192 -------------
193 193
194 194 If you want some code to be run at the beginning of every IPython session, the
195 195 easiest way is to add Python (.py) or IPython (.ipy) scripts to your
196 196 :file:`profile_default/startup/` directory. Files here will be executed as soon
197 197 as the IPython shell is constructed, before any other code or scripts you have
198 198 specified. The files will be run in order of their names, so you can control the
199 199 ordering with prefixes, like ``10-myimports.py``.
200 200
201 201 .. include:: ../links.txt
General Comments 0
You need to be logged in to leave comments. Login now