Reputation: 325
My python import statements have become extremely slow. I'm running python 2.7 locally using the Anaconda package. After importing modules, the code I wrote runs very quickly, it just seems to be the imports that take forever.
As an example, I ran a "tester.py" file with the follow code:
import timeit
x = timeit.timeit('import numpy as np')
print 'imported numpy in %s seconds'%x
x = timeit.timeit('import pandas as pd')
print 'imported pandas in %s seconds'%x
x = timeit.timeit('from Tkinter import Frame,Tk, Label, Checkbutton')
print 'imported Tkinter in %s seconds'%x
x = timeit.timeit('from tkFileDialog import askopenfilenames, asksaveasfilename')
print 'imported tkFileDialog in %s seconds'%x
x = timeit.timeit('import tkMessageBox')
print 'imported tkMessageBox in %s seconds'%x
x = timeit.timeit('import os')
print 'imported os in %s seconds'%x
Output from the command line is:
C:\Users\***\AppData\Local\Continuum\Anaconda>C:\Users\***\Desktop\tester.py
imported numpy in 5.22607264113 seconds
imported pandas in 13.7990192174 seconds
imported Tkinter in 3.95690550577 seconds
imported tkFileDialog in 3.62803133249 seconds
imported tkMessageBox in 1.50766849631 seconds
imported os in 1.87009742139 seconds
How can I diagnose what is happening and/or speed up the imports? I'm not really sure where to start.... Maybe re-installing Anaconda? Any insights or ideas are much appreciated.
Upvotes: 3
Views: 3873
Reputation: 11
Your use of timeit is not correct, you should pass number = 1 so that import statement is executed just once. By default timeit passes "number" as 1000000. No wonder it takes few seconds to import a module million times.
Having said that, python imports are in general slow and you will find some good advice on how to speed up imports (lazy imports, pre-compiled modules etc) on stackoverflow and other online sources.
Upvotes: 1