At this time, I don't have a specific goal in mind other than understanding the border cases I encountered in the past that still puzzle me (latest piece of code I adapted in MPY is a serial driver for the Dongbu HerkuleX DRS-0602 servo).
import myscript_file works indeed, but I disagree it's not like typing the script directly into the REPL: after a import myscript_file command, accessing a myscript_file variable is done typing myscript_file.variable not variable only.
from myscript_file import * sounded more promising to me, but it is still different from typing the code directly into the REPL, as illustrated with the short code of my previous post. I am puzzled...
[Solved] Noob has questions about exec() and import()
Re: Noob has questions about exec() and import()
Here are some of the findings I made while investigating the issues relative to this post:
- from mod import * is different from entering the content of mod.py directly into the REPL due to name binding.
- exec("string") is not implemented the same way in MPY as in CPY.
In MPY it always runs the string code in the __main__ namespace, whereas in CPY this done in the local context (e.g when nested in a function or module).
- locals() in MPY works differently than in CPY and returns the same as globals() (most of the time).
- from mod import * is different from entering the content of mod.py directly into the REPL due to name binding.
- exec("string") is not implemented the same way in MPY as in CPY.
In MPY it always runs the string code in the __main__ namespace, whereas in CPY this done in the local context (e.g when nested in a function or module).
- locals() in MPY works differently than in CPY and returns the same as globals() (most of the time).