repl.rst 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. The MicroPython Interactive Interpreter Mode (aka REPL)
  2. =======================================================
  3. This section covers some characteristics of the MicroPython Interactive
  4. Interpreter Mode. A commonly used term for this is REPL (read-eval-print-loop)
  5. which will be used to refer to this interactive prompt.
  6. Auto-indent
  7. -----------
  8. When typing python statements which end in a colon (for example if, for, while)
  9. then the prompt will change to three dots (...) and the cursor will be indented
  10. by 4 spaces. When you press return, the next line will continue at the same
  11. level of indentation for regular statements or an additional level of indentation
  12. where appropriate. If you press the backspace key then it will undo one
  13. level of indentation.
  14. If your cursor is all the way back at the beginning, pressing RETURN will then
  15. execute the code that you've entered. The following shows what you'd see
  16. after entering a for statement (the underscore shows where the cursor winds up):
  17. >>> for i in range(30):
  18. ... _
  19. If you then enter an if statement, an additional level of indentation will be
  20. provided:
  21. >>> for i in range(30):
  22. ... if i > 3:
  23. ... _
  24. Now enter ``break`` followed by RETURN and press BACKSPACE:
  25. >>> for i in range(30):
  26. ... if i > 3:
  27. ... break
  28. ... _
  29. Finally type ``print(i)``, press RETURN, press BACKSPACE and press RETURN again:
  30. >>> for i in range(30):
  31. ... if i > 3:
  32. ... break
  33. ... print(i)
  34. ...
  35. 0
  36. 1
  37. 2
  38. 3
  39. >>>
  40. Auto-indent won't be applied if the previous two lines were all spaces. This
  41. means that you can finish entering a compound statement by pressing RETURN
  42. twice, and then a third press will finish and execute.
  43. Auto-completion
  44. ---------------
  45. While typing a command at the REPL, if the line typed so far corresponds to
  46. the beginning of the name of something, then pressing TAB will show
  47. possible things that could be entered. For example, first import the machine
  48. module by entering ``import machine`` and pressing RETURN.
  49. Then type ``m`` and press TAB and it should expand to ``machine``.
  50. Enter a dot ``.`` and press TAB again. You should see something like:
  51. >>> machine.
  52. __name__ info unique_id reset
  53. bootloader freq rng idle
  54. sleep deepsleep disable_irq enable_irq
  55. Pin
  56. The word will be expanded as much as possible until multiple possibilities exist.
  57. For example, type ``machine.Pin.AF3`` and press TAB and it will expand to
  58. ``machine.Pin.AF3_TIM``. Pressing TAB a second time will show the possible
  59. expansions:
  60. >>> machine.Pin.AF3_TIM
  61. AF3_TIM10 AF3_TIM11 AF3_TIM8 AF3_TIM9
  62. >>> machine.Pin.AF3_TIM
  63. Interrupting a running program
  64. ------------------------------
  65. You can interrupt a running program by pressing Ctrl-C. This will raise a KeyboardInterrupt
  66. which will bring you back to the REPL, providing your program doesn't intercept the
  67. KeyboardInterrupt exception.
  68. For example:
  69. >>> for i in range(1000000):
  70. ... print(i)
  71. ...
  72. 0
  73. 1
  74. 2
  75. 3
  76. ...
  77. 6466
  78. 6467
  79. 6468
  80. Traceback (most recent call last):
  81. File "<stdin>", line 2, in <module>
  82. KeyboardInterrupt:
  83. >>>
  84. Paste Mode
  85. ----------
  86. If you want to paste some code into your terminal window, the auto-indent feature
  87. will mess things up. For example, if you had the following python code: ::
  88. def foo():
  89. print('This is a test to show paste mode')
  90. print('Here is a second line')
  91. foo()
  92. and you try to paste this into the normal REPL, then you will see something like
  93. this:
  94. >>> def foo():
  95. ... print('This is a test to show paste mode')
  96. ... print('Here is a second line')
  97. ... foo()
  98. ...
  99. Traceback (most recent call last):
  100. File "<stdin>", line 3
  101. IndentationError: unexpected indent
  102. If you press Ctrl-E, then you will enter paste mode, which essentially turns off
  103. the auto-indent feature, and changes the prompt from ``>>>`` to ``===``. For example:
  104. >>>
  105. paste mode; Ctrl-C to cancel, Ctrl-D to finish
  106. === def foo():
  107. === print('This is a test to show paste mode')
  108. === print('Here is a second line')
  109. === foo()
  110. ===
  111. This is a test to show paste mode
  112. Here is a second line
  113. >>>
  114. Paste Mode allows blank lines to be pasted. The pasted text is compiled as if
  115. it were a file. Pressing Ctrl-D exits paste mode and initiates the compilation.
  116. Soft Reset
  117. ----------
  118. A soft reset will reset the python interpreter, but tries not to reset the
  119. method by which you're connected to the MicroPython board (USB-serial, or Wifi).
  120. You can perform a soft reset from the REPL by pressing Ctrl-D, or from your python
  121. code by executing: ::
  122. machine.soft_reset()
  123. For example, if you reset your MicroPython board, and you execute a dir()
  124. command, you'd see something like this:
  125. >>> dir()
  126. ['__name__', 'pyb']
  127. Now create some variables and repeat the dir() command:
  128. >>> i = 1
  129. >>> j = 23
  130. >>> x = 'abc'
  131. >>> dir()
  132. ['j', 'x', '__name__', 'pyb', 'i']
  133. >>>
  134. Now if you enter Ctrl-D, and repeat the dir() command, you'll see that your
  135. variables no longer exist:
  136. .. code-block:: python
  137. PYB: sync filesystems
  138. PYB: soft reboot
  139. MicroPython v1.5-51-g6f70283-dirty on 2015-10-30; PYBv1.0 with STM32F405RG
  140. Type "help()" for more information.
  141. >>> dir()
  142. ['__name__', 'pyb']
  143. >>>
  144. The special variable _ (underscore)
  145. -----------------------------------
  146. When you use the REPL, you may perform computations and see the results.
  147. MicroPython stores the results of the previous statement in the variable _ (underscore).
  148. So you can use the underscore to save the result in a variable. For example:
  149. >>> 1 + 2 + 3 + 4 + 5
  150. 15
  151. >>> x = _
  152. >>> x
  153. 15
  154. >>>
  155. Raw Mode
  156. --------
  157. Raw mode is not something that a person would normally use. It is intended for
  158. programmatic use. It essentially behaves like paste mode with echo turned off.
  159. Raw mode is entered using Ctrl-A. You then send your python code, followed by
  160. a Ctrl-D. The Ctrl-D will be acknowledged by 'OK' and then the python code will
  161. be compiled and executed. Any output (or errors) will be sent back. Entering
  162. Ctrl-B will leave raw mode and return the the regular (aka friendly) REPL.
  163. The ``tools/pyboard.py`` program uses the raw REPL to execute python files on the
  164. MicroPython board.