Python file can't import another py file

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
tim3385
Posts: 8
Joined: Fri Mar 22, 2019 11:14 am

Python file can't import another py file

Post by tim3385 » Mon Aug 05, 2019 3:41 am

I embed javascript port into my emscripten application. mp_js_do_str works fine in my c code.
And that pyexec_file invoke a python file is fine too. i.e. upy works in web application. But The python file can't "import" another module which is also a python file in the same directory.
I test it in emscripten MEMFS root directory "/".

This is the gist below(Sorry, I don't know how to insert text as code in this forum):
```
FILE* fp = NULL;
fp = fopen("t2.py", "w");
fprintf(fp, "import woxiao\nwoxiao.HUDPrint(\"print by t2.py\")\n");
fclose(fp);

fp = fopen("t1.py", "w");
//fprintf(fp, "import woxiao\nwoxiao.HUDPrint(\"print by t1.py\")\ntry:\nimport t2\nwoxiao.HUDPrint(\"+ import t2 done\")\npass\nexcept ImportError:\nwoxiao.HUDPrint(\"- import t2 failed\")\n");
fprintf(fp, "import woxiao\nwoxiao.HUDPrint(\"print by t1.py\")\nimport t2\n");
fclose(fp);

//pyexec_file ("t2.py"); //works fine
pyexec_file ("t1.py"); // can't import t2
```
ps. "woxiao.HUDPrint" is just a debug print function in my own "woxiao" module which is extended in my C code. And it works fine.

tim3385
Posts: 8
Joined: Fri Mar 22, 2019 11:14 am

Re: Python file can't import another py file

Post by tim3385 » Thu Nov 07, 2019 4:17 pm

Importing py files in WASM works. But I am not sure if my method is correct. Here's what I did:
1)mp_lexer_new_from_file() in lexer.c was enabled by set "#define MICROPY_READER_POSIX (1)" in ports/javascript/mpconfigport.h. And comment the mp_lexer_new_from_file() define in the ports/javascript/main.c.
2)Copy the implementation of mp_import_stat from ports/unix/main.c into javascript's.
ps. I use upy 1.11.

Post Reply