usage.py 13 KB

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