← index #4972PR #4897
Likely Duplicate · medium · value 1.312
QUERY · ISSUE

Python file can't import another py file

openby fei2020opened 2019-08-05updated 2020-05-09
port-webassembly

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:

	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.

CANDIDATE · PULL REQUEST

port-javascript: what ways about external imports

closedby pmp-popened 2019-07-05updated 2019-08-08
port-webassembly
  • the .__file__ support should be valid for all ports, it was a TODO

  • this PR allow for use of MICROPY_ENABLE_EXTERNAL_IMPORT.

  • it is probably not usefull to optimize file reading, since synchronous file reading shoud be avoided on javascript this is just for script compatibility with other ports.
    files are expected to be found whole in memfs, embed them via --preload-file or file packager.

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