2019-03-10 Slow imports and code to investigateΒΆ

I notice that the following code was very slow:

import pyquickhelper.__main__ as m
m.main(['clean_files', '--help'])

I wrote the following code to detect which import was the reason behind in order to delay this import wherever possible. I found two. The first is pip which I could easily delay. The second one was surprinsigly sphinxcontrib.websupport which takes 75% of the import time. The code relies on the fact that Python does not import twice the same package.

<<<

def f1():
    from sphinxcontrib.websupport.utils import is_commentable


def f2():
    from sphinx.application import Sphinx


def myf():
    import pyquickhelper.__main__ as m
    m.main(['clean_files', '--help'])


import cProfile
import re
import cProfile
import pstats
import io
from pstats import SortKey
pr = cProfile.Profile()
pr.enable()

f1()
f2()
myf()

pr.disable()
s = io.StringIO()
sortby = SortKey.CUMULATIVE
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
ps.print_stats()
print(s.getvalue())

>>>

    
    [runpythonerror]
    Traceback (most recent call last):
      File "<stdin>", line 24, in <module>
      File "<stdin>", line 8, in f1
    ModuleNotFoundError: No module named 'sphinxcontrib.websupport'