Forum: Poser Python Scripting


Subject: Useful Code Snippets

structure opened this issue on Jun 27, 2019 ยท 94 posts


structure posted Sun, 27 March 2022 at 11:22 AM Forum Coordinator

Install a missing module from inside a running python script, and rerun the script (using rich as an example )


class basic_functions:
    def module_exists(self, module_name):
        from pkgutil import iter_modules
        return module_name in (name for loader, name, ispkg in iter_modules())


if basic_functions().module_exists("rich"):
    from rich.align import Align
    from rich.console import Console
else:
    try:
        from subprocess import check_call
        import os
        import sys
        check_call([sys.executable, "-m", "pip", "install", 'rich'])
        import importlib
        from rich.align import Align
        from rich.console import Console
        importlib.reload(rich)
        os.execv(sys.argv[0], sys.argv)
    except:
        pass


rich allows you to create outputs like this :

Locked Out