usage.py 14 KB

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