##// END OF EJS Templates
Merge pull request #13055 from meeseeksmachine/auto-backport-of-pr-13052-on-7.x...
Merge pull request #13055 from meeseeksmachine/auto-backport-of-pr-13052-on-7.x Backport PR #13052 on branch 7.x (Fix path handling in `pip` line magic)

File last commit:

r25145:2069edca
r26652:4dcdce3f merge
Show More
tutorial.rst
282 lines | 11.6 KiB | text/x-rst | RstLexer
Brian E Granger
Massive reorganization of the IPython documentation. It is now ready to be hacked on by users. ...
r1258 .. _tutorial:
======================
Thomas Kluyver
Split material from tutorial page into introduction and tips pages.
r4087 Introducing IPython
Brian E Granger
Massive reorganization of the IPython documentation. It is now ready to be hacked on by users. ...
r1258 ======================
Thomas Kluyver
Split material from tutorial page into introduction and tips pages.
r4087 You don't need to know anything beyond Python to start using IPython – just type
commands as you would at the standard Python prompt. But IPython can do much
more than the standard prompt. Some key features are described here. For more
information, check the :ref:`tips page <tips>`, or look at examples in the
Paul Ivanov
major doc update for 1.0 release...
r12098 `IPython cookbook <https://github.com/ipython/ipython/wiki/Cookbook%3A-Index>`_.
Brian Granger
Work on documentation....
r2276
Matthias Bussonnier
fix instlal link
r25145 If you haven't done that yet see :ref:`how to install ipython <install>`.
Matthias Bussonnier
[Doc] Rework tutorial
r23478
Thomas Kluyver
Split material from tutorial page into introduction and tips pages.
r4087 If you've never used Python before, you might want to look at `the official
tutorial <http://docs.python.org/tutorial/>`_ or an alternative, `Dive into
techassetskris
Fix #11820 - Broken links in tutorial.
r25143 Python <https://www.diveinto.org/python3/table-of-contents.html>`_.
Brian Granger
Work on documentation....
r2276
Matthias Bussonnier
[Doc] Rework tutorial
r23478 Start IPython by issuing the ``ipython`` command from your shell, you should be
greeted by the following::
Python 3.6.0
Type 'copyright', 'credits' or 'license' for more information
IPython 6.0.0.dev -- An enhanced Interactive Python. Type '?' for help.
In [1]:
Unlike the Python REPL, you will see that the input prompt is ``In [N]:``
instead of ``>>>``. The number ``N`` in the prompt will be used later in this
tutorial but should usually not impact the computation.
You should be able to type single line expressions and press enter to evaluate
them. If an expression is incomplete, IPython will automatically detect this and
Paul Ivanov
minor edits
r23663 add a new line when you press :kbd:`Enter` instead of executing right away.
Matthias Bussonnier
[Doc] Rework tutorial
r23478
Min RK
[doc] minor edits
r23481 Feel free to explore multi-line text input. Unlike many other REPLs, with
Matthias Bussonnier
[Doc] Rework tutorial
r23478 IPython you can use the up and down arrow keys when editing multi-line
code blocks.
Min RK
[doc] minor edits
r23481 Here is an example of a longer interaction with the IPython REPL,
Matthias Bussonnier
Some extra info about tab completion in documentation.
r23662 which we often refer to as an IPython *session* ::
Matthias Bussonnier
[Doc] Rework tutorial
r23478
In [1]: print('Hello IPython')
Hello IPython
In [2]: 21 * 2
Out[2]: 42
In [3]: def say_hello(name):
...: print('Hello {name}'.format(name=name))
...:
Matthias Bussonnier
Some extra info about tab completion in documentation.
r23662 We won't get into details right now, but you may notice a few differences to
the standard Python REPL. First, your code should be syntax-highlighted as you
type. Second, you will see that some results will have an ``Out[N]:`` prompt,
while some other do not. We'll come to this later.
Paul Ivanov
minor edits
r23663 Depending on the exact command you are typing you might realize that sometimes
:kbd:`Enter` will add a new line, and sometimes it will execute the current
Matthias Bussonnier
Some extra info about tab completion in documentation.
r23662 statement. IPython tries to guess what you are doing, so most of the time you
should not have to care. Though if by any chance IPython does not the right
thing you can force execution of the current code block by pressing in sequence
Paul Ivanov
minor edits
r23663 :kbd:`Esc` and :kbd:`Enter`. You can also force the insertion of a new line at
the position of the cursor by using :kbd:`Ctrl-o`.
Matthias Bussonnier
[Doc] Rework tutorial
r23478
The four most helpful commands
==============================
Paul Ivanov
major doc update for 1.0 release...
r12098
The four most helpful commands, as well as their brief description, is shown
to you in a banner, every time you start IPython:
========== =========================================================
command description
========== =========================================================
? Introduction and overview of IPython's features.
%quickref Quick reference.
help Python's own help system.
object? Details about 'object', use 'object??' for extra details.
========== =========================================================
Thomas Kluyver
Split material from tutorial page into introduction and tips pages.
r4087 Tab completion
==============
Brian E Granger
Massive reorganization of the IPython documentation. It is now ready to be hacked on by users. ...
r1258
Thomas Kluyver
Split material from tutorial page into introduction and tips pages.
r4087 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>``
Matthias Bussonnier
One more pass on the docs....
r22598 to view the object's attributes. Besides Python objects and keywords, tab
completion also works on file and directory names.
Brian E Granger
Massive reorganization of the IPython documentation. It is now ready to be hacked on by users. ...
r1258
Matthias Bussonnier
Some extra info about tab completion in documentation.
r23662 Starting with IPython 6.0, if ``jedi`` is installed, IPython will try to pull
Paul Ivanov
minor edits
r23663 completions from Jedi as well. This allows to not only inspect currently
Matthias Bussonnier
Some extra info about tab completion in documentation.
r23662 existing objects, but also to infer completion statically without executing
Paul Ivanov
minor edits
r23663 code. There is nothing particular need to get this to work, simply use tab
completion on more complex expressions like the following::
Matthias Bussonnier
Some extra info about tab completion in documentation.
r23662
Marc Richter
Update tutorial.rst...
r23782 >>> data = ['Number of users', 123456]
Matthias Bussonnier
Some extra info about tab completion in documentation.
r23662 ... data[0].<tab>
IPython and Jedi will be able to infer that ``data[0]`` is actually a string
and should show relevant completions like ``upper()``, ``lower()`` and other
Paul Ivanov
minor edits
r23663 string methods. You can use the :kbd:`Tab` key to cycle through completions,
and while a completion is highlighted, its type will be shown as well.
Matthias Bussonnier
Show signature with Jedi....
r23753 When the type of the completion is a function, the completer will also show the
signature of the function when highlighted.
Matthias Bussonnier
Some extra info about tab completion in documentation.
r23662
Thomas Kluyver
Split material from tutorial page into introduction and tips pages.
r4087 Exploring your objects
======================
Brian E Granger
Massive reorganization of the IPython documentation. It is now ready to be hacked on by users. ...
r1258
Thomas Kluyver
Split material from tutorial page into introduction and tips pages.
r4087 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. To get specific information on an object, you
can use the magic commands ``%pdoc``, ``%pdef``, ``%psource`` and ``%pfile``
Brian E Granger
Massive reorganization of the IPython documentation. It is now ready to be hacked on by users. ...
r1258
Paul Ivanov
major doc update for 1.0 release...
r12098 .. _magics_explained:
Thomas Kluyver
Split material from tutorial page into introduction and tips pages.
r4087 Magic functions
===============
Brian E Granger
Massive reorganization of the IPython documentation. It is now ready to be hacked on by users. ...
r1258
Thomas Kluyver
Split material from tutorial page into introduction and tips pages.
r4087 IPython has a set of predefined 'magic functions' that you can call with a
Fernando Perez
Update the main documentation with new magics API....
r7009 command line style syntax. There are two kinds of magics, line-oriented and
Matthias Bussonnier
One more pass on the docs....
r22598 cell-oriented. **Line magics** are prefixed with the ``%`` character and work
much like OS command-line calls: they get as an argument the rest of the line,
where arguments are passed without parentheses or quotes. **Lines magics** can
Carol Willing
Minor edits to tutorial doc
r22603 return results and can be used in the right hand side of an assignment. **Cell
Matthias Bussonnier
One more pass on the docs....
r22598 magics** are prefixed with a double ``%%``, and they are functions that get as
an argument not only the rest of the line, but also the lines below it in a
separate argument.
Fernando Perez
Update the main documentation with new magics API....
r7009
Matthias Bussonnier
One more pass on the docs....
r22598 Magics are useful as convenient functions where Python syntax is not the most
natural one, or when one want to embed invalid python syntax in their work flow.
Paul Ivanov
minor edits
r23663 The following examples show how to call the built-in :magic:`timeit` magic, both
Matthias Bussonnier
One more pass on the docs....
r22598 in line and cell mode::
Fernando Perez
Update the main documentation with new magics API....
r7009
In [1]: %timeit range(1000)
100000 loops, best of 3: 7.76 us per loop
In [2]: %%timeit x = range(10000)
Matthias Bussonnier
[Doc] Rework tutorial
r23478 ...: max(x)
...:
Fernando Perez
Update the main documentation with new magics API....
r7009 1000 loops, best of 3: 223 us per loop
Paul Ivanov
minor edits
r23663 The built-in magics include:
Brian E Granger
Massive reorganization of the IPython documentation. It is now ready to be hacked on by users. ...
r1258
Matthias Bussonnier
One more pass on the docs....
r22598 - Functions that work with code: :magic:`run`, :magic:`edit`, :magic:`save`,
:magic:`macro`, :magic:`recall`, etc.
- Functions which affect the shell: :magic:`colors`, :magic:`xmode`,
Matthias Bussonnier
Undeprecate autoindent...
r24498 :magic:`automagic`, etc.
Matthias Bussonnier
One more pass on the docs....
r22598
- Other functions such as :magic:`reset`, :magic:`timeit`,
:cellmagic:`writefile`, :magic:`load`, or :magic:`paste`.
Brian E Granger
Massive reorganization of the IPython documentation. It is now ready to be hacked on by users. ...
r1258
Matthias Bussonnier
One more pass on the docs....
r22598 You can always call magics using the ``%`` prefix, and if you're calling a line
magic on a line by itself, as long as the identifier is not defined in your
namespace, you can omit even that::
Thomas Kluyver
Split material from tutorial page into introduction and tips pages.
r4087
run thescript.py
Fernando Perez
Update the main documentation with new magics API....
r7009
Matthias Bussonnier
One more pass on the docs....
r22598 You can toggle this behavior by running the :magic:`automagic` magic. Cell
magics must always have the ``%%`` prefix.
Paul Ivanov
major doc update for 1.0 release...
r12098
Fernando Perez
Update the main documentation with new magics API....
r7009 A more detailed explanation of the magic system can be obtained by calling
``%magic``, and for more details on any magic function, call ``%somemagic?`` to
read its docstring. To see all the available magic functions, call
``%lsmagic``.
Thomas Kluyver
Split material from tutorial page into introduction and tips pages.
r4087
Paul Ivanov
major doc update for 1.0 release...
r12098 .. seealso::
Matthias Bussonnier
One more pass on the docs....
r22598 The :ref:`magic` section of the documentation goes more in depth into how
the magics works and how to define your own, and :doc:`magics` for a list of
built-in magics.
Thomas Kluyver
Start using magic roles for cross-referencing
r18297
Paul Ivanov
major doc update for 1.0 release...
r12098 `Cell magics`_ example notebook
Thomas Kluyver
Split material from tutorial page into introduction and tips pages.
r4087 Running and Editing
-------------------
Brian E Granger
Massive reorganization of the IPython documentation. It is now ready to be hacked on by users. ...
r1258
Matthias Bussonnier
One more pass on the docs....
r22598 The :magic:`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
Paul Ivanov
major doc update for 1.0 release...
r12098 from disk each time, changes you make to it are reflected immediately (unlike
Matthias Bussonnier
One more pass on the docs....
r22598 imported modules, which have to be specifically reloaded). IPython also includes
:ref:`dreload <dreload>`, a recursive reload function.
Thomas Kluyver
Split material from tutorial page into introduction and tips pages.
r4087
Paul Ivanov
major doc update for 1.0 release...
r12098 ``%run`` has special flags for timing the execution of your scripts (-t), or
for running them under the control of either Python's pdb debugger (-d) or
Thomas Kluyver
Split material from tutorial page into introduction and tips pages.
r4087 profiler (-p).
Paul Ivanov
minor edits
r23663 The :magic:`edit` command gives a reasonable approximation of multi-line editing,
Thomas Kluyver
Split material from tutorial page into introduction and tips pages.
r4087 by invoking your favorite editor on the spot. IPython will execute the
Matthias Bussonnier
One more pass on the docs....
r22598 code you type in there as if it were typed interactively. Note that for
Carol Willing
Minor edits to tutorial doc
r22603 :magic:`edit` to work, the call to startup your editor has to be a blocking
call. In a GUI environment, your editor likely will have such an option.
Thomas Kluyver
Split material from tutorial page into introduction and tips pages.
r4087
Debugging
---------
Thomas Kluyver
Start using magic roles for cross-referencing
r18297 After an exception occurs, you can call :magic:`debug` to jump into the Python
debugger (pdb) and examine the problem. Alternatively, if you call :magic:`pdb`,
Thomas Kluyver
Split material from tutorial page into introduction and tips pages.
r4087 IPython will automatically start the debugger on any uncaught exception. You can
Matthias Bussonnier
One more pass on the docs....
r22598 print variables, see code, execute statements and even walk up and down the call
stack to track down the true source of the problem. This can be an efficient way
to develop and debug code, in many cases eliminating the need for print
Thomas Kluyver
Improve and update interactive docs.
r5440 statements or external debugging tools.
Thomas Kluyver
Split material from tutorial page into introduction and tips pages.
r4087
You can also step through a program from the beginning by calling
``%run -d theprogram.py``.
History
=======
IPython stores both the commands you enter, and the results it produces. You
can easily go through previous commands with the up- and down-arrow keys, or
access your history in more sophisticated ways.
Thomas Kluyver
Improve and update interactive docs.
r5440 Input and output history are kept in variables called ``In`` and ``Out``, keyed
by the prompt numbers, e.g. ``In[4]``. The last three objects in output history
are also kept in variables named ``_``, ``__`` and ``___``.
Thomas Kluyver
Split material from tutorial page into introduction and tips pages.
r4087
You can use the ``%history`` magic function to examine past input and output.
Input history from previous sessions is saved in a database, and IPython can be
configured to save output history.
Several other magic functions can use your input history, including ``%edit``,
``%rerun``, ``%recall``, ``%macro``, ``%save`` and ``%pastebin``. You can use a
standard format to refer to lines::
%pastebin 3 18-20 ~1/1-5
This will take line 3 and lines 18 to 20 from the current session, and lines
1-5 from the previous session.
System shell commands
=====================
Matthias Bussonnier
One more pass on the docs....
r22598 To run any command at the system shell, simply prefix it with ``!``, e.g.::
Thomas Kluyver
Split material from tutorial page into introduction and tips pages.
r4087
!ping www.bbc.co.uk
You can capture the output into a Python list, e.g.: ``files = !ls``. To pass
the values of Python variables or expressions to system commands, prefix them
anatoly techtonik
tutorial.rst: Mention braces for variable interpolation
r24038 with $: ``!grep -rF $pattern ipython/*`` or wrap in `{braces}`. See :ref:`our
shell section <system_shell_access>` for more details.
Thomas Kluyver
Start using magic roles for cross-referencing
r18297
Brian E Granger
Massive reorganization of the IPython documentation. It is now ready to be hacked on by users. ...
r1258 Define your own system aliases
------------------------------
Matthias Bussonnier
One more pass on the docs....
r22598 It's 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 :magic:`pushd`,
:magic:`popd` and :magic:`dhist`) and via direct :magic:`cd`. The latter keeps a
history of visited directories and allows you to go to any previously visited
one.
Brian E Granger
Massive reorganization of the IPython documentation. It is now ready to be hacked on by users. ...
r1258
Thomas Kluyver
Split material from tutorial page into introduction and tips pages.
r4087 Configuration
=============
Brian E Granger
Massive reorganization of the IPython documentation. It is now ready to be hacked on by users. ...
r1258
Thomas Kluyver
Tutorial doc should link to user config intro...
r13714 Much of IPython can be tweaked through :doc:`configuration </config/intro>`.
Paul Ivanov
major doc update for 1.0 release...
r12098 To get started, use the command ``ipython profile create`` to produce the
default config files. These will be placed in
Thomas Kluyver
Update doc references to .config/ipython
r13417 :file:`~/.ipython/profile_default`, and contain comments explaining
Paul Ivanov
major doc update for 1.0 release...
r12098 what the various options do.
Brian E Granger
Massive reorganization of the IPython documentation. It is now ready to be hacked on by users. ...
r1258
Thomas Kluyver
Split material from tutorial page into introduction and tips pages.
r4087 Profiles allow you to use IPython for different tasks, keeping separate config
files and history for each one. More details in :ref:`the profiles section
<profiles>`.
Brian Granger
Cleanup of docs....
r2275
Thomas Kluyver
Fix various warnings for broken references etc
r21578 .. _startup_files:
MinRK
add startup files to docs
r5248 Startup Files
-------------
If you want some code to be run at the beginning of every IPython session, the
easiest way is to add Python (.py) or IPython (.ipy) scripts to your
Thomas Kluyver
Simplify description of startup files in introductory docs.
r5441 :file:`profile_default/startup/` directory. Files here will be executed as soon
as the IPython shell is constructed, before any other code or scripts you have
specified. The files will be run in order of their names, so you can control the
ordering with prefixes, like ``10-myimports.py``.
MinRK
add startup files to docs
r5248
Paul Ivanov
major doc update for 1.0 release...
r12098 .. include:: ../links.txt