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