loading dynamically python


the context:
I will write some mission/event in my game. And because, some of them are very scenaristic, I think I may happen, I will have to write the sequencing of an event in a separate python script.

the needing:
Depending of the event, I have to load dynamically a python script, and to run it. I use directory to describe modules, and I have for example, shimstar/event/event.py (which contains class shimEvent).
How to load it and use it? Is this behavior still works while being pdeployed?

Finally, it is a good way to do to script event specifically? is there any way to do? What is your experience?

I’ve used the import() built-in. Pass a string naming your script as an argument. so, myscript = import(“script”). this is assuming that your script is in the same directory, else you’ll have to mess with python’s search path docs.python.org/2/install/index. … earch-path


Either that or put all your dynamically importable classes into one package and after importing the package use getattr(package, “modulename”) on it. If you put an import statement into the init.py file of a package you can also use getattr() with a class directly.