[Solved] Noob has questions about exec() and import()

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
User avatar
sebi
Posts: 48
Joined: Tue Mar 29, 2016 9:36 pm
Location: France

Re: Noob has questions about exec() and import()

Post by sebi » Mon Apr 01, 2019 8:19 am

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...

User avatar
sebi
Posts: 48
Joined: Tue Mar 29, 2016 9:36 pm
Location: France

Re: Noob has questions about exec() and import()

Post by sebi » Sat Apr 06, 2019 6:36 pm

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).

Post Reply