For background information: Go HERE!
I have a very large module that takes things from the internet, other built-in scripts and etc. Depending on factors such as network speed, memory and then compiling lists and stuff like that, the import time can vary from between 25 seconds and 90 seconds. I have used the following code to track how long the module takes to import:
def importTime():
import time
startTime = time.time()
import tms # This is the name of my module
print("Time taken {}".format(time.time() - startTime)))
When I run this:
>>> importTime()
Loading Module. This may take up to 60 seconds. # This is my module output
Time taken 31.49
This is what I want to have happen:
>>> import tms
Loading Module. This may take up to 60 seconds.
Time taken: 31.49 seconds
Here's my issue. This is a function which I have to define before importing my module. What I need to be able to do is have my module be able to do this upon startup. I've taken a look at this question, but it's the same concept. Does anyone have any ideas?
Its usually undesirable to do large amounts of work at module import - that plays havoc with documentation scanners, IDE's, unit test frameworks and the like. Ideally
tms
should be rewritten to do its work in a function. But to solve your problem, just write a short module that imports your module. You could even name ittms
and rename the original so that other importers get the same functionality (if that's desirable).tmsx.py
Now just import tmsx
You can overload the
__import__
function which is called when a module is imported: