Execute file when REPL is entered

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Execute file when REPL is entered

Post by devnull » Tue Jan 11, 2022 12:26 am

Is it possible to detect when the REPL is logged into, my guess is no.

When a user logs into the REPL I want to trigger a soft reboot, or even import a file.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Execute file when REPL is entered

Post by dhylands » Wed Jan 12, 2022 6:34 pm

I guess it depends on what exactly you mean by "logged into the REPL" since the REPL doesn't have any real notion of logging in.

The REPL will be entered as soon as main.py exits, or otherwise throws an error.

So you could put a try/catch around your main.py execution and if the catch executes then it will execute just before the REPL is entered.

Detecting that a user (i.e. host pc) is connected to the REPL is much more difficult (and probably impossible in general). Some devices can detect if a USB cable is plugged in, but that doesn't mean that any program running on the host actually has the serial port open.

Post Reply