Splash Screen and importing libraries

I have a program that imports a bunch of modules before calling the main loop, and thus it takes a while before the main window is shown. I would like to show a splash screen during initialization, but I can’t find a way to load the libraries during that. They can’t be at the beginning of the program, because otherwise the splash screen will be shown only after they’ve been loaded, defeating its purpose. If I load them elsewhere, they are not visible to the rest of the program. I tried declaring the module names as globals, and then loading the libraries inside a function, but that isn’t working. Is there any way around this?

Try How to create a splash screen while loading (Phoenix), experiment with it and see if it fits the bill.


import threading
import time
import importlib

def load_modules():
    global module1, module2
    module1 = importlib.import_module('module1')
    module2 = importlib.import_module('module2')

def show_splash_screen():
    # Your splash screen code here

splash_thread = threading.Thread(target=show_splash_screen)


# Continue with the main program

Thanks, will try!

How does that work with partial imports (i.e. from xyz import abc)?

To handle partial imports with importlib, use getattr after importing the module. Here’s an example:

def load_modules():
    global abc_from_xyz
    xyz = importlib.import_module('xyz')
    abc_from_xyz = getattr(xyz, 'abc')