How to force import a module
SebastienTainon opened this issue · comments
Hello,
How can I tell Skulpt to import a module before running my code? I would like users to be able to run a program like
mycustomfunction('param')
without having them writing from mycustommodule import *
at the beginning of every program. I could add it myself but then it would shift the line numbers of the code which breaks debugging, so it's not a viable solution.
I tried with:
Sk.importModule('mycustommodule', undefined, true);
var $loc = [];
var $module1 = Sk.builtin.__import__('mycustommodule', Sk.globals, $loc, ['*']);
Sk.importStar($module1, $loc, Sk.globals);
but I don't know have to have access to Skulpt globals and locals...
NB: I have already created the module and my code works when I manually add from mycustommodule import *
, my question is about how to do this programmatically before running the code.
Thanks!
If you're using Sk.importMainWithBody
you could prefix the body with from mycustommodule import *\n
before running the user's code.
You could also add each attribute from your custom module to Sk.builtins
which is the namespace for the builtins that is used in compile code.
Prefixing works greatly but like I said, it shifts all the line number of the code. Sk.builtins
is perfect however, thanks!