Python class inheritence cross files
Hello,
I have been playing with microstudio with python. I am building mutiple class in different files with class inheritance.
I find that often the program a fails to run due to class names not found. I think it somehow depend on which file I had opened.
For instance (I could share the project):
object file:
class Object():
pass
foo file:
class Foo(Object):
pass
File main:
def init():
f = Foo()
How does the python code gets run? does it get concatenated and is there way to influence the concatenation ?
(I would assume it is as there are no import between files)
Thank you
In MicroStudio, when using Python, the way code is executed and the structure of multiple files differ from traditional Python module imports.
MicroStudio does not use the standard Python import statement to manage dependencies between files. All source files are loaded at startup and contribute to a shared global context. This means functions, classes, and variables defined in one file are accessible in others without explicit imports.
You shouldn't rely on a specific loading order for your files in MicroStudio, according to a microStudio forum discussion. Instead, it's recommended to define functions and classes globally across your files but avoid making direct calls or instantiations immediately upon execution.
The suggested way to manage your code is to create global functions, classes, and variables that are then used in other files. You should initiate your program's execution by calling functions or instantiating classes only after the init() function is called. This signal confirms that all code has been loaded and is ready for use.
While not technically string concatenation in the Python sense, you can think of it as MicroStudio effectively "concatenating" your code files into a single execution context at runtime. The individual files are not directly merged into one large string of code, but their content is loaded and becomes globally available, similar to the effect of having all the code in a single file.
Leverage the global scope by defining functions and classes directly in your separate files, making them accessible everywhere.
Use the init() function to start your program's logic once all files are loaded. This provides a controlled entry point.
For larger and reusable code, you can create and import libraries, which are sets of functions or classes stored in a lib folder within your project. This acts as a form of controlled inclusion, where the entire library's source code is copied into your project under a specific folder (e.g., lib/lib_author/lib_slug/*) when imported. I hope this information helps. :)
The problem still persist after making use I have no code executed before the init.
I still feel like the content loading order is a problem. If
foo file is loaded first, the Object class will not be available. I can see errors like:
Object class doesn't exists
NameError: name 'Detector' is not defined, in file "projectile" at line 2
NameError: name 'Detector' is not defined, in file "chest" at line 2
NameError: name 'BaseObject' is not defined, in file "mimic" at line 2
NameError: name 'Detector' is not defined, in file "coin" at line 4
NameError: name 'Detector' is not defined, in file "trap" at line 3
For the library route, any example project/doc to look at ?
For lib/lib_author/lib_slug/*, would the import be import lib_author.lib_slug or just import slug or import lib_slug ?
Thanks!
Try moving all your core classes (Object, Detector, BaseObject) into one file like _00_core.py so it loads first. Then, import those classes at the top of every other script that uses them:
from _00_core import Object, Detector
This should fix the "class not defined" errors. Also, library imports may not work the same as Python modules — Microstudio might not support nested import lib_author.lib_slug, so it’s better to keep shared classes in your main project for now.