#3019·brython

'import f from m' syntax inside of an exec on compiled code fails

Author: benjie-gitCreated Sep 23, 2026Updated Sep 23, 2026

This one is pretty obscure, but my weird code hits it! (This used to work, but broke in the last ~5 months.)

This works:

python
code = "import random; print(random.randint(1,10))"
exec(code)

This works:

python
code = "from random import randint; print(randint(1,10))"
exec(code)

This works:

python
code = "import random; print(random.randint(1,10))"
astCode = compile(code, "<string>", "exec")
exec(astCode)

But this fails:

python
code = "from random import randint; print(randint(1,10))"
astCode = compile(code, "<string>", "exec")
exec(astCode)

with the error:

TypeError: n.startsWith is not a function
    at g (brython.js:1:570820)
    at b (brython.js:1:571121)
    at k (brython.js:1:571392)
    at e.ast.ImportFrom.to_js (brython.js:1:617772)
    at e.js_from_ast (brython.js:1:640619)
    at M (brython.js:1:576931)
    at e.ast.Module.to_js (brython.js:1:623989)
    at e.js_from_root (brython.js:1:640315)
    at r.eval (brython.js:1:208479)
    at r.exec (brython.js:1:209965)