Dat Chu
Dat Chu

Reputation: 11140

Organize heavy python imports

About 25% of my code depends on the modules: Traits, tvtk, ... which are quite heavy to import. It typically takes a good 2 seconds on my machine (and more on other).

My modules are organized as the following

mainmodule
|--submodule1
|--submodule2
   |--subsubmodule1
   |--subsubmodule2
|--submodule3
|--submodule4
   |--subsubmodule1
   |--subsubmodule2

In these, the submodule1 and submodule2 use Traits. That means 75% of the time, if I call import mainmodule, I will have to wait for the heavy modules to be imported but then they won't be used.

How do I organize my imports so that I can lower my import time?

Maybe there is a way to do something like:

import mainmodule

and have

mainmodule
|--submodule3
|--submodule4
   |--subsubmodule1
   |--subsubmodule2

And only call:

import mainmodule.heavy

to have everything

Upvotes: 2

Views: 1027

Answers (2)

Jason Orendorff
Jason Orendorff

Reputation: 45116

It sounds like what you want is a way so that importing mainmodule doesn't automatically import submodule1 and submodule2, which take a long time to load.

That's pretty easy, actually. You can import submodule1 and submodule2 only in functions that need them. Or move those functions into a separate module called mainmodule_heavy.py.

(Or you could hack the Python module system to load modules lazily. But that kind of hack tends to cause problems, and it sounds unnecessary for your case.)

Upvotes: 3

Alex Leach
Alex Leach

Reputation: 1201

You can put some code like this within a function / module:-

def heavy():
    global x
    global y
    import x, y

def mainmodule():
    if heavy not in globals():
        import heavy

Actually, this wouldn't work within the same program, as a function cannot be imported. Also, you'd want to check for a string within globals, not the module itself. So, instead:-

def heavy():
    global x
    global y
    import x, y

def mainmodule():
    if 'x' not in globals() or 'y' not in globals():
        heavy()

Upvotes: 3

Related Questions