Repository: RustPython/RustPython (language: rust)

Issue:
Importing modules not working with WASI
I tried to import a module that's just next to another module, e.g.

```
main.py
utils.py
```

If I try to import `utils` in `main.py`

```
import utils

utils.sayHello()
```

it fails and says

```
ModuleNotFoundError: No module named 'utils'
```

Any idea why that is not working?


Discussion / hints:
@youknowone Any idea what the issue could be? Is it maybe that paths are not correctly resolved when compiled to WASI?
Hmm. I confirmed `open('f.py')` works but `import f` doesn't work.
@d3lm I am still not used to be developing on wasm/wasi. Do you have recommendation for debuggers setup?
Interesting. Here I think there's something going on with maybe path resolution but I am not entirely sure. Debugging is a little difficult with Rust because AFAIK dwarf symbols don't fully work with Rust. So the best is to add logs which is quite tedious 😑 😅 I ll have a look. Can you point me to the right places where module resolution happens?
The first common entrance is here: https://github.com/RustPython/RustPython/blob/main/vm/src/frame.rs#L1138-L1148
But the inside is tricky because the actual body of import is implemented in `importlib` the python library.
You will need to look in both rust and python side to get what's exactly happening. Here is the list of import related stuff:

- `vm/src/frame.rs` `fn import`
- `vm/src/vm/mod.rs` `fn import_inner`
- `vm/src/stdlib/imp.rs` (= `_imp` module in Python side)
- `vm/Lib/python_builtins/_frozen_importlib.py`
- `vm/Lib/python_builtins/_frozen_importlib_external.py`
- `Lib/importlib`

Not sure about current state, but I once found editing files under `vm/Lib/python_builtins/` sometimes doesn't invalidate build cache well. I needed to do clean build when it had problems at that time.
Hm ok. I can try and see if I can find something. I really want to add RustPython to WebContainer to allow people to code in Python inside the browser without using any cloud VMs. It'd be a really cool playground for you as well and we could add it to the README even, but we have to figure out what the issue here is. Maybe you can have a look as well?

Fix patch:
diff --git a/Cargo.toml b/Cargo.toml
index 8fd494a67f..cd10c10f92 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -80,10 +80,10 @@ unicode_names2 = { version = "0.6.0", git = "https://github.com/youknowone/unico
 widestring = "0.5.1"
 
 [features]
-default = ["threading", "stdlib", "zlib", "importlib", "encodings"]
+default = ["threading", "stdlib", "zlib", "importlib"]
 importlib = ["rustpython-vm/importlib"]
 encodings = ["rustpython-vm/encodings"]
-stdlib = ["rustpython-stdlib", "rustpython-pylib"]
+stdlib = ["rustpython-stdlib", "rustpython-pylib", "encodings"]
 flame-it = ["rustpython-vm/flame-it", "flame", "flamescope"]
 freeze-stdlib = ["rustpython-vm/freeze-stdlib", "rustpython-pylib?/freeze-stdlib"]
 jit = ["rustpython-vm/jit"]
diff --git a/vm/Cargo.toml b/vm/Cargo.toml
index d25096bd4e..fb3ab1e554 100644
--- a/vm/Cargo.toml
+++ b/vm/Cargo.toml
@@ -14,7 +14,7 @@ importlib = []
 encodings = ["importlib"]
 vm-tracing-logging = []
 flame-it = ["flame", "flamer"]
-freeze-stdlib = []
+freeze-stdlib = ["encodings"]
 jit = ["rustpython-jit"]
 threading = ["rustpython-common/threading"]
 compiler = ["parser", "codegen", "rustpython-compiler"]


Test patch: