usage.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. # -*- coding: utf-8 -*-
  2. """Usage information for the main IPython applications.
  3. """
  4. #-----------------------------------------------------------------------------
  5. # Copyright (C) 2008-2011 The IPython Development Team
  6. # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
  7. #
  8. # Distributed under the terms of the BSD License. The full license is in
  9. # the file COPYING, distributed as part of this software.
  10. #-----------------------------------------------------------------------------
  11. import sys
  12. from IPython.core import release
  13. cl_usage = """\
  14. =========
  15. IPython
  16. =========
  17. Tools for Interactive Computing in Python
  18. =========================================
  19. A Python shell with automatic history (input and output), dynamic object
  20. introspection, easier configuration, command completion, access to the
  21. system shell and more. IPython can also be embedded in running programs.
  22. Usage
  23. ipython [subcommand] [options] [-c cmd | -m mod | file] [--] [arg] ...
  24. If invoked with no options, it executes the file and exits, passing the
  25. remaining arguments to the script, just as if you had specified the same
  26. command with python. You may need to specify `--` before args to be passed
  27. to the script, to prevent IPython from attempting to parse them. If you
  28. specify the option `-i` before the filename, it will enter an interactive
  29. IPython session after running the script, rather than exiting. Files ending
  30. in .py will be treated as normal Python, but files ending in .ipy can
  31. contain special IPython syntax (magic commands, shell expansions, etc.).
  32. Almost all configuration in IPython is available via the command-line. Do
  33. `ipython --help-all` to see all available options. For persistent
  34. configuration, look into your `ipython_config.py` configuration file for
  35. details.
  36. This file is typically installed in the `IPYTHONDIR` directory, and there
  37. is a separate configuration directory for each profile. The default profile
  38. directory will be located in $IPYTHONDIR/profile_default. IPYTHONDIR
  39. defaults to to `$HOME/.ipython`. For Windows users, $HOME resolves to
  40. C:\\Users\\YourUserName in most instances.
  41. To initialize a profile with the default configuration file, do::
  42. $> ipython profile create
  43. and start editing `IPYTHONDIR/profile_default/ipython_config.py`
  44. In IPython's documentation, we will refer to this directory as
  45. `IPYTHONDIR`, you can change its default location by creating an
  46. environment variable with this name and setting it to the desired path.
  47. For more information, see the manual available in HTML and PDF in your
  48. installation, or online at http://ipython.org/documentation.html.
  49. """
  50. interactive_usage = """
  51. IPython -- An enhanced Interactive Python
  52. =========================================
  53. IPython offers a fully compatible replacement for the standard Python
  54. interpreter, with convenient shell features, special commands, command
  55. history mechanism and output results caching.
  56. At your system command line, type 'ipython -h' to see the command line
  57. options available. This document only describes interactive features.
  58. MAIN FEATURES
  59. -------------
  60. * Access to the standard Python help with object docstrings and the Python
  61. manuals. Simply type 'help' (no quotes) to invoke it.
  62. * Magic commands: type %magic for information on the magic subsystem.
  63. * System command aliases, via the %alias command or the configuration file(s).
  64. * Dynamic object information:
  65. Typing ?word or word? prints detailed information about an object. Certain
  66. long strings (code, etc.) get snipped in the center for brevity.
  67. Typing ??word or word?? gives access to the full information without
  68. snipping long strings. Strings that are longer than the screen are printed
  69. through the less pager.
  70. The ?/?? system gives access to the full source code for any object (if
  71. available), shows function prototypes and other useful information.
  72. If you just want to see an object's docstring, type '%pdoc object' (without
  73. quotes, and without % if you have automagic on).
  74. * Tab completion in the local namespace:
  75. At any time, hitting tab will complete any available python commands or
  76. variable names, and show you a list of the possible completions if there's
  77. no unambiguous one. It will also complete filenames in the current directory.
  78. * Search previous command history in multiple ways:
  79. - Start typing, and then use arrow keys up/down or (Ctrl-p/Ctrl-n) to search
  80. through the history items that match what you've typed so far.
  81. - Hit Ctrl-r: opens a search prompt. Begin typing and the system searches
  82. your history for lines that match what you've typed so far, completing as
  83. much as it can.
  84. - %hist: search history by index.
  85. * Persistent command history across sessions.
  86. * Logging of input with the ability to save and restore a working session.
  87. * System shell with !. Typing !ls will run 'ls' in the current directory.
  88. * The reload command does a 'deep' reload of a module: changes made to the
  89. module since you imported will actually be available without having to exit.
  90. * Verbose and colored exception traceback printouts. See the magic xmode and
  91. xcolor functions for details (just type %magic).
  92. * Input caching system:
  93. IPython offers numbered prompts (In/Out) with input and output caching. All
  94. input is saved and can be retrieved as variables (besides the usual arrow
  95. key recall).
  96. The following GLOBAL variables always exist (so don't overwrite them!):
  97. _i: stores previous input.
  98. _ii: next previous.
  99. _iii: next-next previous.
  100. _ih : a list of all input _ih[n] is the input from line n.
  101. Additionally, global variables named _i<n> are dynamically created (<n>
  102. being the prompt counter), such that _i<n> == _ih[<n>]
  103. For example, what you typed at prompt 14 is available as _i14 and _ih[14].
  104. You can create macros which contain multiple input lines from this history,
  105. for later re-execution, with the %macro function.
  106. The history function %hist allows you to see any part of your input history
  107. by printing a range of the _i variables. Note that inputs which contain
  108. magic functions (%) appear in the history with a prepended comment. This is
  109. because they aren't really valid Python code, so you can't exec them.
  110. * Output caching system:
  111. For output that is returned from actions, a system similar to the input
  112. cache exists but using _ instead of _i. Only actions that produce a result
  113. (NOT assignments, for example) are cached. If you are familiar with
  114. Mathematica, IPython's _ variables behave exactly like Mathematica's %
  115. variables.
  116. The following GLOBAL variables always exist (so don't overwrite them!):
  117. _ (one underscore): previous output.
  118. __ (two underscores): next previous.
  119. ___ (three underscores): next-next previous.
  120. Global variables named _<n> are dynamically created (<n> being the prompt
  121. counter), such that the result of output <n> is always available as _<n>.
  122. Finally, a global dictionary named _oh exists with entries for all lines
  123. which generated output.
  124. * Directory history:
  125. Your history of visited directories is kept in the global list _dh, and the
  126. magic %cd command can be used to go to any entry in that list.
  127. * Auto-parentheses and auto-quotes (adapted from Nathan Gray's LazyPython)
  128. 1. Auto-parentheses
  129. Callable objects (i.e. functions, methods, etc) can be invoked like
  130. this (notice the commas between the arguments)::
  131. In [1]: callable_ob arg1, arg2, arg3
  132. and the input will be translated to this::
  133. callable_ob(arg1, arg2, arg3)
  134. This feature is off by default (in rare cases it can produce
  135. undesirable side-effects), but you can activate it at the command-line
  136. by starting IPython with `--autocall 1`, set it permanently in your
  137. configuration file, or turn on at runtime with `%autocall 1`.
  138. You can force auto-parentheses by using '/' as the first character
  139. of a line. For example::
  140. In [1]: /globals # becomes 'globals()'
  141. Note that the '/' MUST be the first character on the line! This
  142. won't work::
  143. In [2]: print /globals # syntax error
  144. In most cases the automatic algorithm should work, so you should
  145. rarely need to explicitly invoke /. One notable exception is if you
  146. are trying to call a function with a list of tuples as arguments (the
  147. parenthesis will confuse IPython)::
  148. In [1]: zip (1,2,3),(4,5,6) # won't work
  149. but this will work::
  150. In [2]: /zip (1,2,3),(4,5,6)
  151. ------> zip ((1,2,3),(4,5,6))
  152. Out[2]= [(1, 4), (2, 5), (3, 6)]
  153. IPython tells you that it has altered your command line by
  154. displaying the new command line preceded by -->. e.g.::
  155. In [18]: callable list
  156. -------> callable (list)
  157. 2. Auto-Quoting
  158. You can force auto-quoting of a function's arguments by using ',' as
  159. the first character of a line. For example::
  160. In [1]: ,my_function /home/me # becomes my_function("/home/me")
  161. If you use ';' instead, the whole argument is quoted as a single
  162. string (while ',' splits on whitespace)::
  163. In [2]: ,my_function a b c # becomes my_function("a","b","c")
  164. In [3]: ;my_function a b c # becomes my_function("a b c")
  165. Note that the ',' MUST be the first character on the line! This
  166. won't work::
  167. In [4]: x = ,my_function /home/me # syntax error
  168. """
  169. interactive_usage_min = """\
  170. An enhanced console for Python.
  171. Some of its features are:
  172. - Tab completion in the local namespace.
  173. - Logging of input, see command-line options.
  174. - System shell escape via ! , eg !ls.
  175. - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.)
  176. - Keeps track of locally defined variables via %who, %whos.
  177. - Show object information with a ? eg ?x or x? (use ?? for more info).
  178. """
  179. quick_reference = r"""
  180. IPython -- An enhanced Interactive Python - Quick Reference Card
  181. ================================================================
  182. obj?, obj?? : Get help, or more help for object (also works as
  183. ?obj, ??obj).
  184. ?foo.*abc* : List names in 'foo' containing 'abc' in them.
  185. %magic : Information about IPython's 'magic' % functions.
  186. Magic functions are prefixed by % or %%, and typically take their arguments
  187. without parentheses, quotes or even commas for convenience. Line magics take a
  188. single % and cell magics are prefixed with two %%.
  189. Example magic function calls:
  190. %alias d ls -F : 'd' is now an alias for 'ls -F'
  191. alias d ls -F : Works if 'alias' not a python name
  192. alist = %alias : Get list of aliases to 'alist'
  193. cd /usr/share : Obvious. cd -<tab> to choose from visited dirs.
  194. %cd?? : See help AND source for magic %cd
  195. %timeit x=10 : time the 'x=10' statement with high precision.
  196. %%timeit x=2**100
  197. x**100 : time 'x**100' with a setup of 'x=2**100'; setup code is not
  198. counted. This is an example of a cell magic.
  199. System commands:
  200. !cp a.txt b/ : System command escape, calls os.system()
  201. cp a.txt b/ : after %rehashx, most system commands work without !
  202. cp ${f}.txt $bar : Variable expansion in magics and system commands
  203. files = !ls /usr : Capture sytem command output
  204. files.s, files.l, files.n: "a b c", ['a','b','c'], 'a\nb\nc'
  205. History:
  206. _i, _ii, _iii : Previous, next previous, next next previous input
  207. _i4, _ih[2:5] : Input history line 4, lines 2-4
  208. exec _i81 : Execute input history line #81 again
  209. %rep 81 : Edit input history line #81
  210. _, __, ___ : previous, next previous, next next previous output
  211. _dh : Directory history
  212. _oh : Output history
  213. %hist : Command history of current session.
  214. %hist -g foo : Search command history of (almost) all sessions for 'foo'.
  215. %hist -g : Command history of (almost) all sessions.
  216. %hist 1/2-8 : Command history containing lines 2-8 of session 1.
  217. %hist 1/ ~2/ : Command history of session 1 and 2 sessions before current.
  218. %hist ~8/1-~6/5 : Command history from line 1 of 8 sessions ago to
  219. line 5 of 6 sessions ago.
  220. %edit 0/ : Open editor to execute code with history of current session.
  221. Autocall:
  222. f 1,2 : f(1,2) # Off by default, enable with %autocall magic.
  223. /f 1,2 : f(1,2) (forced autoparen)
  224. ,f 1 2 : f("1","2")
  225. ;f 1 2 : f("1 2")
  226. Remember: TAB completion works in many contexts, not just file names
  227. or python names.
  228. The following magic functions are currently available:
  229. """
  230. quick_guide = """\
  231. ? -> Introduction and overview of IPython's features.
  232. %quickref -> Quick reference.
  233. help -> Python's own help system.
  234. object? -> Details about 'object', use 'object??' for extra details.
  235. """
  236. default_banner_parts = [
  237. 'Python %s\n' % (sys.version.split('\n')[0],),
  238. 'Type "copyright", "credits" or "license" for more information.\n\n',
  239. 'IPython {version} -- An enhanced Interactive Python.\n'.format(
  240. version=release.version,
  241. ),
  242. quick_guide
  243. ]
  244. default_banner = ''.join(default_banner_parts)
  245. # deprecated GUI banner
  246. default_gui_banner = '\n'.join([
  247. 'DEPRECATED: IPython.core.usage.default_gui_banner is deprecated and will be removed',
  248. default_banner,
  249. ])