As has been well-documented (SO question here, Python import documentation, etc), python caches its imports and generates compiled .pyc files so that you only have to import files once. However, not all of the information is cached; I don't understand exactly how the caching works. I would like to more completely cache the imports so that files that take a long time to import can be sped up. For example, say I have these files, which look like this:
main.py
/src
mysrc.py
main.py
import time
start = time.time()
from src.mysrc import TesterClass
print ("elapsed:", time.time() - start)
mysrc.py
import time
time.sleep(2) # SLEEPING FOR 2 SECONDS HERE
class TesterClass:
def __init__(self):
pass
Then if I call main.py from the command line, it will show that just over 2 seconds elapses every time, no matter if the .pyc files have already been generated.
It seems like there would be a way to keep track of the class if it has already been imported, so that subsequent calls to import the class would be much quicker.
Is this possible?