[Python] Plugin - passing globals to a module

Discussão em 'Python' iniciado por Stack, Março 9, 2025.

  1. Stack

    Stack Membro Participativo

    I have Python Module system that I am trying to create.

    I have followed a previous example and it seems to work sell, except one thing.

    The basic 'manager' does the following:

    NAME_PY='myplugin.py'
    def load_plugin( self, dirname ):
    filename = os.path.join( dirname, NAME_PY )
    module_name = filename[:-3]
    file_path = os.path.join(self.plugin_dir, filename)
    spec = importlib.util.spec_from_file_location(module_name, file_path)
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)


    All of this works and works nicely. I am at the next step. I am trying to model this Python construct.

    if __name__ == '__main__':
    print("This is the main module")
    else:
    print("this is not the main python file, it is being imported")


    In my solution I want to use that same idiom and do this:

    if __name__ == 'plugin':
    print("Loaded from a plugin"
    if __name__ == '__main__':
    print("This is a plugin, it is not an standalone python file")


    As I understand it, __name__ is a member of the dictionary globals(). And I just need to set that before I call: spec.loader.exec_module(module)

    But I cannot seem to find the 'global' thing I can set my __name__ to.

    Suggestions?

    Continue reading...

Compartilhe esta Página