pyodide: Importing dynamically written module gives ModuleNotFoundError
In #419, @mdboom suggests the following way of importing a dynamically created file (without the two commented lines):
import sys
sys.path.insert(0, '.')
# with open("bar.txt", "w") as fd:
# fd.write("bar")
with open("module.py", "w") as fd:
fd.write("foo = 42")
import module
module.foo
As is, this works fine.
However, with the two lines uncommented this yields ModuleNotFoundError: No module named 'module'
. This is unexpected, as creating bar.txt
seems unrelated to the rest of the code.
Adding import os; print(os.listdir("."))
just before the import shows that both files were indeed created.
Also, with the order of the two with
blocks swapped it does work as expected.
Tested with v0.15.0 on Firefox 78.0.2.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 20 (20 by maintainers)
Commits related to this issue
- Add test case for #737 — committed to rth/pyodide by rth 4 years ago
- Add test case for #737 — committed to rth/pyodide by rth 4 years ago
I believe so. The bug was with the modification timestamp impacting caching. The rest is just expected Python behavior. One shoud either add directory far enough in
sys.path
or open file with specified encoding or import_bootlocale
first.