QUERY · ISSUE
BUG: sys.modules['__main__'] not pointing __import__('__main__')
proposed-closeneeds-info
cpython automatically gives access to sys.modules[__name__] from module '__main__' and returns value of __import__('__main__').
micropython does not and forces to use__import__(__name__) which is not a good way to get a module by fully qualified name ( eg when getting a dot package name you only get toplevel ).
CANDIDATE · ISSUE
exec('import module') doesn't add module to scope when used in imported file
needs-info
Maybe this is intended or known as a difference with CPython but I looked around and didn't see any documentation related to it.
To reproduce (tested on unix-like ports only): create these 3 files:
foo.py:
foo = 1
bar.py:
def Bar():
exec('import foo')
print(foo.foo)
if __name__ == "__main__":
Bar()
importbar.py:
import bar
bar.Bar()
Running micropython bar.py works ok and prints 1.
Running micropython importbar.py also imports foo but doesn't add it to the scope somehow:
Traceback (most recent call last):
File "importbar.py", line 2, in <module>
File "bar.py", line 3, in Bar
NameError: name 'foo' is not defined