Know if are run from REPL or from main.py #9245
Replies: 3 comments 5 replies
-
I don't think this is possible with any built-in variable or anything. main.py is a bit special because it isn't imported. Probably the best thing to do is have your main.py set a variable while it is running and always clear it when finishing (or being interrupted) main.py: def main():
# all my program code runs from here
try:
in_main = True
main()
finally:
in_main = False You could set |
Beta Was this translation helpful? Give feedback.
-
I don’t know if that helps in your case, but you can distinguish whether main.py is being run from MicroPython startup or from if __name__ == '__main__':
# called from startup
else:
# __name__ == 'main'
# called from import (Example) |
Beta Was this translation helpful? Give feedback.
-
Expanding on @cwalther's approach... You could try to differentiate between imported and lauched mode of executing a module (like we do in CPython). I'd put my logic into separate file (let's call it therealthing.py): def main(as_main):
print("as_main", as_main)
if __name__ == "__main__":
main(as_main=True) When testing the logic in Thonny, I would run this file directly and this would make For automatic running, I would create main.py with following content: import therealthing
therealthing.main(as_main=False) |
Beta Was this translation helpful? Give feedback.
-
Hi all,
I would like to tell if the program was started from local main.py file or being run through REPL, in this case with Thonny.
My goal is to automatically enable Dev variables when I run code via REPL (through Thonny), but use production settings otherwise.
I was wondering if there is a constant or variable set in micropython for this. I did not find this yet. The other possibility that I searched for is the current filename code is being run from, still didn't find anything yet for this.
Is there any way to achieve this?
Thank you,
Dave
Beta Was this translation helpful? Give feedback.
All reactions