Show More
@@ -1,260 +1,258 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 haven't done that yet see `how to install ipython <install>`_ . |
|
13 | If you haven't done that yet see `how to install ipython <install>`_ . | |
14 |
|
14 | |||
15 | If you've never used Python before, you might want to look at `the official |
|
15 | If you've never used Python before, you might want to look at `the official | |
16 | tutorial <http://docs.python.org/tutorial/>`_ or an alternative, `Dive into |
|
16 | tutorial <http://docs.python.org/tutorial/>`_ or an alternative, `Dive into | |
17 | Python <http://diveintopython.net/toc/index.html>`_. |
|
17 | Python <http://diveintopython.net/toc/index.html>`_. | |
18 |
|
18 | |||
19 | Start IPython by issuing the ``ipython`` command from your shell, you should be |
|
19 | Start IPython by issuing the ``ipython`` command from your shell, you should be | |
20 | greeted by the following:: |
|
20 | greeted by the following:: | |
21 |
|
21 | |||
22 | Python 3.6.0 |
|
22 | Python 3.6.0 | |
23 | Type 'copyright', 'credits' or 'license' for more information |
|
23 | Type 'copyright', 'credits' or 'license' for more information | |
24 | IPython 6.0.0.dev -- An enhanced Interactive Python. Type '?' for help. |
|
24 | IPython 6.0.0.dev -- An enhanced Interactive Python. Type '?' for help. | |
25 |
|
25 | |||
26 | In [1]: |
|
26 | In [1]: | |
27 |
|
27 | |||
28 |
|
28 | |||
29 | Unlike the Python REPL, you will see that the input prompt is ``In [N]:`` |
|
29 | Unlike the Python REPL, you will see that the input prompt is ``In [N]:`` | |
30 | instead of ``>>>``. The number ``N`` in the prompt will be used later in this |
|
30 | instead of ``>>>``. The number ``N`` in the prompt will be used later in this | |
31 | tutorial but should usually not impact the computation. |
|
31 | tutorial but should usually not impact the computation. | |
32 |
|
32 | |||
33 | You should be able to type single line expressions and press enter to evaluate |
|
33 | You should be able to type single line expressions and press enter to evaluate | |
34 | them. If an expression is incomplete, IPython will automatically detect this and |
|
34 | them. If an expression is incomplete, IPython will automatically detect this and | |
35 |
add a new line when you press ``Enter`` instead of e |
|
35 | add a new line when you press ``Enter`` instead of executing right away. | |
36 |
|
36 | |||
37 |
Feel free to explore multi-line text |
|
37 | Feel free to explore multi-line text input. Unlike many other REPLs, with | |
38 | IPython you can use the up and down arrow keys when editing multi-line |
|
38 | IPython you can use the up and down arrow keys when editing multi-line | |
39 | code blocks. |
|
39 | code blocks. | |
40 |
|
40 | |||
41 |
Here is an example of a longer interaction with the IPython REPL |
|
41 | Here is an example of a longer interaction with the IPython REPL, | |
42 | to as an IPython _session_ :: |
|
42 | which we often refer to as an IPython _session_ :: | |
43 |
|
43 | |||
44 | In [1]: print('Hello IPython') |
|
44 | In [1]: print('Hello IPython') | |
45 | Hello IPython |
|
45 | Hello IPython | |
46 |
|
46 | |||
47 | In [2]: 21 * 2 |
|
47 | In [2]: 21 * 2 | |
48 | Out[2]: 42 |
|
48 | Out[2]: 42 | |
49 |
|
49 | |||
50 | In [3]: def say_hello(name): |
|
50 | In [3]: def say_hello(name): | |
51 | ...: print('Hello {name}'.format(name=name)) |
|
51 | ...: print('Hello {name}'.format(name=name)) | |
52 | ...: |
|
52 | ...: | |
53 |
|
53 | |||
54 |
We won't get into details right now, but |
|
54 | We won't get into details right now, but you may notice a few differences to the standard Python REPL. | |
55 |
|
|
55 | First, your code should be syntax-highlighted as you type. | |
56 | you type. |
|
|||
57 |
|
||||
58 | Second, you will see that some results will have an ``Out[N]:`` prompt, while |
|
56 | Second, you will see that some results will have an ``Out[N]:`` prompt, while | |
59 | some other do not. We'll come to this later. |
|
57 | some other do not. We'll come to this later. | |
60 |
|
58 | |||
61 | The four most helpful commands |
|
59 | The four most helpful commands | |
62 | ============================== |
|
60 | ============================== | |
63 |
|
61 | |||
64 | The four most helpful commands, as well as their brief description, is shown |
|
62 | The four most helpful commands, as well as their brief description, is shown | |
65 | to you in a banner, every time you start IPython: |
|
63 | to you in a banner, every time you start IPython: | |
66 |
|
64 | |||
67 | ========== ========================================================= |
|
65 | ========== ========================================================= | |
68 | command description |
|
66 | command description | |
69 | ========== ========================================================= |
|
67 | ========== ========================================================= | |
70 | ? Introduction and overview of IPython's features. |
|
68 | ? Introduction and overview of IPython's features. | |
71 | %quickref Quick reference. |
|
69 | %quickref Quick reference. | |
72 | help Python's own help system. |
|
70 | help Python's own help system. | |
73 | object? Details about 'object', use 'object??' for extra details. |
|
71 | object? Details about 'object', use 'object??' for extra details. | |
74 | ========== ========================================================= |
|
72 | ========== ========================================================= | |
75 |
|
73 | |||
76 | Tab completion |
|
74 | Tab completion | |
77 | ============== |
|
75 | ============== | |
78 |
|
76 | |||
79 | Tab completion, especially for attributes, is a convenient way to explore the |
|
77 | Tab completion, especially for attributes, is a convenient way to explore the | |
80 | structure of any object you're dealing with. Simply type ``object_name.<TAB>`` |
|
78 | structure of any object you're dealing with. Simply type ``object_name.<TAB>`` | |
81 | to view the object's attributes. Besides Python objects and keywords, tab |
|
79 | to view the object's attributes. Besides Python objects and keywords, tab | |
82 | completion also works on file and directory names. |
|
80 | completion also works on file and directory names. | |
83 |
|
81 | |||
84 | Exploring your objects |
|
82 | Exploring your objects | |
85 | ====================== |
|
83 | ====================== | |
86 |
|
84 | |||
87 | Typing ``object_name?`` will print all sorts of details about any object, |
|
85 | Typing ``object_name?`` will print all sorts of details about any object, | |
88 | including docstrings, function definition lines (for call arguments) and |
|
86 | including docstrings, function definition lines (for call arguments) and | |
89 | constructor details for classes. To get specific information on an object, you |
|
87 | constructor details for classes. To get specific information on an object, you | |
90 | can use the magic commands ``%pdoc``, ``%pdef``, ``%psource`` and ``%pfile`` |
|
88 | can use the magic commands ``%pdoc``, ``%pdef``, ``%psource`` and ``%pfile`` | |
91 |
|
89 | |||
92 | .. _magics_explained: |
|
90 | .. _magics_explained: | |
93 |
|
91 | |||
94 | Magic functions |
|
92 | Magic functions | |
95 | =============== |
|
93 | =============== | |
96 |
|
94 | |||
97 | IPython has a set of predefined 'magic functions' that you can call with a |
|
95 | IPython has a set of predefined 'magic functions' that you can call with a | |
98 | command line style syntax. There are two kinds of magics, line-oriented and |
|
96 | command line style syntax. There are two kinds of magics, line-oriented and | |
99 | cell-oriented. **Line magics** are prefixed with the ``%`` character and work |
|
97 | cell-oriented. **Line magics** are prefixed with the ``%`` character and work | |
100 | much like OS command-line calls: they get as an argument the rest of the line, |
|
98 | much like OS command-line calls: they get as an argument the rest of the line, | |
101 | where arguments are passed without parentheses or quotes. **Lines magics** can |
|
99 | where arguments are passed without parentheses or quotes. **Lines magics** can | |
102 | return results and can be used in the right hand side of an assignment. **Cell |
|
100 | return results and can be used in the right hand side of an assignment. **Cell | |
103 | magics** are prefixed with a double ``%%``, and they are functions that get as |
|
101 | magics** are prefixed with a double ``%%``, and they are functions that get as | |
104 | an argument not only the rest of the line, but also the lines below it in a |
|
102 | an argument not only the rest of the line, but also the lines below it in a | |
105 | separate argument. |
|
103 | separate argument. | |
106 |
|
104 | |||
107 | Magics are useful as convenient functions where Python syntax is not the most |
|
105 | Magics are useful as convenient functions where Python syntax is not the most | |
108 | natural one, or when one want to embed invalid python syntax in their work flow. |
|
106 | natural one, or when one want to embed invalid python syntax in their work flow. | |
109 |
|
107 | |||
110 | The following examples show how to call the builtin :magic:`timeit` magic, both |
|
108 | The following examples show how to call the builtin :magic:`timeit` magic, both | |
111 | in line and cell mode:: |
|
109 | in line and cell mode:: | |
112 |
|
110 | |||
113 | In [1]: %timeit range(1000) |
|
111 | In [1]: %timeit range(1000) | |
114 | 100000 loops, best of 3: 7.76 us per loop |
|
112 | 100000 loops, best of 3: 7.76 us per loop | |
115 |
|
113 | |||
116 | In [2]: %%timeit x = range(10000) |
|
114 | In [2]: %%timeit x = range(10000) | |
117 | ...: max(x) |
|
115 | ...: max(x) | |
118 | ...: |
|
116 | ...: | |
119 | 1000 loops, best of 3: 223 us per loop |
|
117 | 1000 loops, best of 3: 223 us per loop | |
120 |
|
118 | |||
121 | The builtin magics include: |
|
119 | The builtin magics include: | |
122 |
|
120 | |||
123 | - Functions that work with code: :magic:`run`, :magic:`edit`, :magic:`save`, |
|
121 | - Functions that work with code: :magic:`run`, :magic:`edit`, :magic:`save`, | |
124 | :magic:`macro`, :magic:`recall`, etc. |
|
122 | :magic:`macro`, :magic:`recall`, etc. | |
125 |
|
123 | |||
126 | - Functions which affect the shell: :magic:`colors`, :magic:`xmode`, |
|
124 | - Functions which affect the shell: :magic:`colors`, :magic:`xmode`, | |
127 | :magic:`autoindent`, :magic:`automagic`, etc. |
|
125 | :magic:`autoindent`, :magic:`automagic`, etc. | |
128 |
|
126 | |||
129 | - Other functions such as :magic:`reset`, :magic:`timeit`, |
|
127 | - Other functions such as :magic:`reset`, :magic:`timeit`, | |
130 | :cellmagic:`writefile`, :magic:`load`, or :magic:`paste`. |
|
128 | :cellmagic:`writefile`, :magic:`load`, or :magic:`paste`. | |
131 |
|
129 | |||
132 | You can always call magics using the ``%`` prefix, and if you're calling a line |
|
130 | You can always call magics using the ``%`` prefix, and if you're calling a line | |
133 | magic on a line by itself, as long as the identifier is not defined in your |
|
131 | magic on a line by itself, as long as the identifier is not defined in your | |
134 | namespace, you can omit even that:: |
|
132 | namespace, you can omit even that:: | |
135 |
|
133 | |||
136 | run thescript.py |
|
134 | run thescript.py | |
137 |
|
135 | |||
138 | You can toggle this behavior by running the :magic:`automagic` magic. Cell |
|
136 | You can toggle this behavior by running the :magic:`automagic` magic. Cell | |
139 | magics must always have the ``%%`` prefix. |
|
137 | magics must always have the ``%%`` prefix. | |
140 |
|
138 | |||
141 | A more detailed explanation of the magic system can be obtained by calling |
|
139 | A more detailed explanation of the magic system can be obtained by calling | |
142 | ``%magic``, and for more details on any magic function, call ``%somemagic?`` to |
|
140 | ``%magic``, and for more details on any magic function, call ``%somemagic?`` to | |
143 | read its docstring. To see all the available magic functions, call |
|
141 | read its docstring. To see all the available magic functions, call | |
144 | ``%lsmagic``. |
|
142 | ``%lsmagic``. | |
145 |
|
143 | |||
146 | .. seealso:: |
|
144 | .. seealso:: | |
147 |
|
145 | |||
148 | The :ref:`magic` section of the documentation goes more in depth into how |
|
146 | The :ref:`magic` section of the documentation goes more in depth into how | |
149 | the magics works and how to define your own, and :doc:`magics` for a list of |
|
147 | the magics works and how to define your own, and :doc:`magics` for a list of | |
150 | built-in magics. |
|
148 | built-in magics. | |
151 |
|
149 | |||
152 | `Cell magics`_ example notebook |
|
150 | `Cell magics`_ example notebook | |
153 |
|
151 | |||
154 | Running and Editing |
|
152 | Running and Editing | |
155 | ------------------- |
|
153 | ------------------- | |
156 |
|
154 | |||
157 | The :magic:`run` magic command allows you to run any python script and load all |
|
155 | The :magic:`run` magic command allows you to run any python script and load all | |
158 | of its data directly into the interactive namespace. Since the file is re-read |
|
156 | of its data directly into the interactive namespace. Since the file is re-read | |
159 | from disk each time, changes you make to it are reflected immediately (unlike |
|
157 | from disk each time, changes you make to it are reflected immediately (unlike | |
160 | imported modules, which have to be specifically reloaded). IPython also includes |
|
158 | imported modules, which have to be specifically reloaded). IPython also includes | |
161 | :ref:`dreload <dreload>`, a recursive reload function. |
|
159 | :ref:`dreload <dreload>`, a recursive reload function. | |
162 |
|
160 | |||
163 | ``%run`` has special flags for timing the execution of your scripts (-t), or |
|
161 | ``%run`` has special flags for timing the execution of your scripts (-t), or | |
164 | for running them under the control of either Python's pdb debugger (-d) or |
|
162 | for running them under the control of either Python's pdb debugger (-d) or | |
165 | profiler (-p). |
|
163 | profiler (-p). | |
166 |
|
164 | |||
167 | The :magic:`edit` command gives a reasonable approximation of multiline editing, |
|
165 | The :magic:`edit` command gives a reasonable approximation of multiline editing, | |
168 | by invoking your favorite editor on the spot. IPython will execute the |
|
166 | by invoking your favorite editor on the spot. IPython will execute the | |
169 | code you type in there as if it were typed interactively. Note that for |
|
167 | code you type in there as if it were typed interactively. Note that for | |
170 | :magic:`edit` to work, the call to startup your editor has to be a blocking |
|
168 | :magic:`edit` to work, the call to startup your editor has to be a blocking | |
171 | call. In a GUI environment, your editor likely will have such an option. |
|
169 | call. In a GUI environment, your editor likely will have such an option. | |
172 |
|
170 | |||
173 | Debugging |
|
171 | Debugging | |
174 | --------- |
|
172 | --------- | |
175 |
|
173 | |||
176 | After an exception occurs, you can call :magic:`debug` to jump into the Python |
|
174 | After an exception occurs, you can call :magic:`debug` to jump into the Python | |
177 | debugger (pdb) and examine the problem. Alternatively, if you call :magic:`pdb`, |
|
175 | debugger (pdb) and examine the problem. Alternatively, if you call :magic:`pdb`, | |
178 | IPython will automatically start the debugger on any uncaught exception. You can |
|
176 | IPython will automatically start the debugger on any uncaught exception. You can | |
179 | print variables, see code, execute statements and even walk up and down the call |
|
177 | print variables, see code, execute statements and even walk up and down the call | |
180 | stack to track down the true source of the problem. This can be an efficient way |
|
178 | stack to track down the true source of the problem. This can be an efficient way | |
181 | to develop and debug code, in many cases eliminating the need for print |
|
179 | to develop and debug code, in many cases eliminating the need for print | |
182 | statements or external debugging tools. |
|
180 | statements or external debugging tools. | |
183 |
|
181 | |||
184 | You can also step through a program from the beginning by calling |
|
182 | You can also step through a program from the beginning by calling | |
185 | ``%run -d theprogram.py``. |
|
183 | ``%run -d theprogram.py``. | |
186 |
|
184 | |||
187 | History |
|
185 | History | |
188 | ======= |
|
186 | ======= | |
189 |
|
187 | |||
190 | IPython stores both the commands you enter, and the results it produces. You |
|
188 | IPython stores both the commands you enter, and the results it produces. You | |
191 | can easily go through previous commands with the up- and down-arrow keys, or |
|
189 | can easily go through previous commands with the up- and down-arrow keys, or | |
192 | access your history in more sophisticated ways. |
|
190 | access your history in more sophisticated ways. | |
193 |
|
191 | |||
194 | Input and output history are kept in variables called ``In`` and ``Out``, keyed |
|
192 | Input and output history are kept in variables called ``In`` and ``Out``, keyed | |
195 | by the prompt numbers, e.g. ``In[4]``. The last three objects in output history |
|
193 | by the prompt numbers, e.g. ``In[4]``. The last three objects in output history | |
196 | are also kept in variables named ``_``, ``__`` and ``___``. |
|
194 | are also kept in variables named ``_``, ``__`` and ``___``. | |
197 |
|
195 | |||
198 | You can use the ``%history`` magic function to examine past input and output. |
|
196 | You can use the ``%history`` magic function to examine past input and output. | |
199 | Input history from previous sessions is saved in a database, and IPython can be |
|
197 | Input history from previous sessions is saved in a database, and IPython can be | |
200 | configured to save output history. |
|
198 | configured to save output history. | |
201 |
|
199 | |||
202 | Several other magic functions can use your input history, including ``%edit``, |
|
200 | Several other magic functions can use your input history, including ``%edit``, | |
203 | ``%rerun``, ``%recall``, ``%macro``, ``%save`` and ``%pastebin``. You can use a |
|
201 | ``%rerun``, ``%recall``, ``%macro``, ``%save`` and ``%pastebin``. You can use a | |
204 | standard format to refer to lines:: |
|
202 | standard format to refer to lines:: | |
205 |
|
203 | |||
206 | %pastebin 3 18-20 ~1/1-5 |
|
204 | %pastebin 3 18-20 ~1/1-5 | |
207 |
|
205 | |||
208 | This will take line 3 and lines 18 to 20 from the current session, and lines |
|
206 | This will take line 3 and lines 18 to 20 from the current session, and lines | |
209 | 1-5 from the previous session. |
|
207 | 1-5 from the previous session. | |
210 |
|
208 | |||
211 | System shell commands |
|
209 | System shell commands | |
212 | ===================== |
|
210 | ===================== | |
213 |
|
211 | |||
214 | To run any command at the system shell, simply prefix it with ``!``, e.g.:: |
|
212 | To run any command at the system shell, simply prefix it with ``!``, e.g.:: | |
215 |
|
213 | |||
216 | !ping www.bbc.co.uk |
|
214 | !ping www.bbc.co.uk | |
217 |
|
215 | |||
218 | You can capture the output into a Python list, e.g.: ``files = !ls``. To pass |
|
216 | You can capture the output into a Python list, e.g.: ``files = !ls``. To pass | |
219 | the values of Python variables or expressions to system commands, prefix them |
|
217 | the values of Python variables or expressions to system commands, prefix them | |
220 | with $: ``!grep -rF $pattern ipython/*``. See :ref:`our shell section |
|
218 | with $: ``!grep -rF $pattern ipython/*``. See :ref:`our shell section | |
221 | <system_shell_access>` for more details. |
|
219 | <system_shell_access>` for more details. | |
222 |
|
220 | |||
223 | Define your own system aliases |
|
221 | Define your own system aliases | |
224 | ------------------------------ |
|
222 | ------------------------------ | |
225 |
|
223 | |||
226 | It's convenient to have aliases to the system commands you use most often. This |
|
224 | It's convenient to have aliases to the system commands you use most often. This | |
227 | allows you to work seamlessly from inside IPython with the same commands you are |
|
225 | allows you to work seamlessly from inside IPython with the same commands you are | |
228 | used to in your system shell. IPython comes with some pre-defined aliases and a |
|
226 | used to in your system shell. IPython comes with some pre-defined aliases and a | |
229 | complete system for changing directories, both via a stack (see :magic:`pushd`, |
|
227 | complete system for changing directories, both via a stack (see :magic:`pushd`, | |
230 | :magic:`popd` and :magic:`dhist`) and via direct :magic:`cd`. The latter keeps a |
|
228 | :magic:`popd` and :magic:`dhist`) and via direct :magic:`cd`. The latter keeps a | |
231 | history of visited directories and allows you to go to any previously visited |
|
229 | history of visited directories and allows you to go to any previously visited | |
232 | one. |
|
230 | one. | |
233 |
|
231 | |||
234 |
|
232 | |||
235 | Configuration |
|
233 | Configuration | |
236 | ============= |
|
234 | ============= | |
237 |
|
235 | |||
238 | Much of IPython can be tweaked through :doc:`configuration </config/intro>`. |
|
236 | Much of IPython can be tweaked through :doc:`configuration </config/intro>`. | |
239 | To get started, use the command ``ipython profile create`` to produce the |
|
237 | To get started, use the command ``ipython profile create`` to produce the | |
240 | default config files. These will be placed in |
|
238 | default config files. These will be placed in | |
241 | :file:`~/.ipython/profile_default`, and contain comments explaining |
|
239 | :file:`~/.ipython/profile_default`, and contain comments explaining | |
242 | what the various options do. |
|
240 | what the various options do. | |
243 |
|
241 | |||
244 | Profiles allow you to use IPython for different tasks, keeping separate config |
|
242 | Profiles allow you to use IPython for different tasks, keeping separate config | |
245 | files and history for each one. More details in :ref:`the profiles section |
|
243 | files and history for each one. More details in :ref:`the profiles section | |
246 | <profiles>`. |
|
244 | <profiles>`. | |
247 |
|
245 | |||
248 | .. _startup_files: |
|
246 | .. _startup_files: | |
249 |
|
247 | |||
250 | Startup Files |
|
248 | Startup Files | |
251 | ------------- |
|
249 | ------------- | |
252 |
|
250 | |||
253 | If you want some code to be run at the beginning of every IPython session, the |
|
251 | If you want some code to be run at the beginning of every IPython session, the | |
254 | easiest way is to add Python (.py) or IPython (.ipy) scripts to your |
|
252 | easiest way is to add Python (.py) or IPython (.ipy) scripts to your | |
255 | :file:`profile_default/startup/` directory. Files here will be executed as soon |
|
253 | :file:`profile_default/startup/` directory. Files here will be executed as soon | |
256 | as the IPython shell is constructed, before any other code or scripts you have |
|
254 | as the IPython shell is constructed, before any other code or scripts you have | |
257 | specified. The files will be run in order of their names, so you can control the |
|
255 | specified. The files will be run in order of their names, so you can control the | |
258 | ordering with prefixes, like ``10-myimports.py``. |
|
256 | ordering with prefixes, like ``10-myimports.py``. | |
259 |
|
257 | |||
260 | .. include:: ../links.txt |
|
258 | .. include:: ../links.txt |
General Comments 0
You need to be logged in to leave comments.
Login now