Skip to content

Latest commit

 

History

History
99 lines (73 loc) · 1.95 KB

001_python_repl.md

File metadata and controls

99 lines (73 loc) · 1.95 KB

MicroPython REPL

Table of Contents

What is REPL?

With the Python REPL (Read–eval–print loop) you type commands and see the output. The very nice thing is you have features like: Code Suggestions, Auto-Completion, Auto-Indentation, Contextual History and many more.

  • >>> commandline prompt for input (confirmed by ENTER)
  • # are comments and will be ignored
  • output don't start with >>> or #
  • === paste mode (started with CTRL-E)
>>> 10 * 10
100

>>> myname = 'demo-string'
>>> myname
'demo-string'

Start a REPL

SCREEN

$ screen [SERIAL-PORT] 115200
# now press CTRL-D or RST button on device

MPFSHELL

(venv) $ mpfshell -o [SERIAL-PORT]
mpfs [/]> repl

rshell

You can start the REPL via two options

# start REPL directly
(venv) $ rshell -p [SERIAL-PORT] repl

# start serial connection
(venv) $ rshell -p [SERIAL-PORT]

# start REPL
/your/current/path> repl

Usage of REPL

Important control commands:

  • CTRL-A - on a blank line, enter raw REPL mode
  • CTRL-B - on a blank line, enter normal REPL mode
  • CTRL-C - interrupt a running program
  • CTRL-D - on a blank line, do a soft reset of the board
  • CTRL-E - on a blank line, enter paste mode

Some helpful examples

>>> # list all modules
>>> help('modules')

>>> # show help for module
>>> import machine
>>> help(machine)

>>> # show python version
>>> import sys
>>> sys.version
>>> # ... or ...
>>> sys.implementation

>>> # working with directories
>>> import os
>>> os.getcwd()
'/'
>>> os.mkdir('demo')
>>> os.chdir('demo')
>>> os.getcwd()
'/demo'
>>> os.listdir('../')
['boot.py', 'demo', 'main.py']

>>> # soft reset (like CTRL-D)
>>> import machine
>>> machine.soft_reset()

Home | Previous | Next