Python import is very slow - Anaconda python 2.7

My python import statements have become extremely slow. I am using python 2.7 locally using the Anaconda package. After importing the modules, the code that I wrote runs very quickly, it's just an import that takes forever.

As an example, I ran the file "tester.py" with the following 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 

Exit from the command line:

 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 import? I'm not quite sure where to start ... Maybe reinstall Anaconda? Any ideas or ideas are greatly appreciated.

+2
source share
1 answer

Your use of timeit is incorrect, you must pass number = 1 so that the import statement is executed only once. By default, timeit passes the "number" as 1,000,000. It is not surprising that it takes a few seconds to import a module a million times.

Having said that, python imports are generally slow, and you will find some useful tips on how to speed up imports (lazy imports, pre-compiled modules, etc.) to stackoverflow and other online sources.

0
source

Source: https://habr.com/ru/post/1237106/


All Articles