← index #4427Issue #2833
Related · medium · value 0.968
QUERY · ISSUE

BUG: sys.modules['__main__'] not pointing __import__('__main__')

openby pmp-popened 2019-01-25updated 2025-03-03
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

closedby stinosopened 2017-02-01updated 2024-09-30
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

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied