Discord
Login
Community
DARK THEME

import self written modules when using python

When using python as the scripting language, is it possible to create multiple files and import them? I get a ModuleNotFoundError when creating a second file and trying to import it. The Brython documentation claims, that it should be possible. At my standalone version of microstudio, I saw, that the stored script files still have an *.ms suffix, even so python is selected as scripting language. Maybe that's the reason?

Thank you in advance for any help :)

I have zero experience in Python so take the following with a pinch of salt...

The way JavaScript, Lua and Python are integrated in microStudio follows the same paradigm as microScript, that is to say there is no concept of module import ; all the source files are loaded at startup and can populate the global context with functions, classes or other variables ; also you should not make assumptions on the file loading order.

Knowing that, the recommended way to organize your code is to create the main functions and classes as global variables, scattered in as many files as you like, but don't do any direct function calls or class instantiations when the source code is first executed. You code should start calling functions or instantiating classes only when the function init() is called, which is the signal that all your code has been loaded, thus all your global classes and functions are ready to be used.

My guess is that you should just assign your main module object to a global variable and use the same variable from the other files. This way there is no need for import.

Thanks's a lot for your answer. And thank you for all the effort you put into microstudio, it's really amazing! I played around with different engines, from phaser and gamemaker to godot but none of them was so straightforward and nice to use as microstudio :)

Unfortunatelly I couldn't get it to work with your suggestions. Though, I found a workaround. Within the exported HTML5 package I edited the index.html directly. I had to change attribute type to type="text/python" and then could import modules the natural python way.

Now I can edit my files in an python IDE (with code completion etc.) and just have to refresh the browser to see my changes. Gonna play around and investigate this approach some more. For the moment, I'm super happy. Also, because bringing it to Android with cordova worked like a charm.

I know that this is misusing microstudio in a way, but have you thought of also promoting it as kind of a lightweight library? So, again, by no means, please don't take it as an offence, you do a great job.

Kind regards

Nice! Thanks for the feedback :-) no misuse here, you can do whatever works for you! I would be happy to have a closer look at your setup if you want to share, I might learn quite a bit from it. Or maybe you can give me directions on how I could improve the support of Python in microStudio. I would also be interested on a minimum test case of creating and importing a Python module, to see if I can figure out a working solution in microStudio :-)

You can contact me on Discord or by e-mail if you like :-) https://microstudio.dev/about

Thank's a lot for your interest! I pushed a small example on github and invited you :-)

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community