Python导入非常慢-Anaconda python 2.7 [英] Python imports are very slow - Anaconda python 2.7

查看:489
本文介绍了Python导入非常慢-Anaconda python 2.7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的python import语句变得非常慢.我正在使用Anaconda软件包在本地运行python 2.7.导入模块后,我编写的代码运行得非常快,似乎只是永久性的导入.

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.

作为示例,我运行了一个带有以下代码的"tester.py"文件:

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

命令行输出为:

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

如何诊断正在发生的事情和/或加快进口速度?我不太确定从哪里开始....也许重新安装Anaconda?任何见解或想法都将受到赞赏.

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.

推荐答案

您对timeit的使用不正确,您应该传递number = 1,以便import语句仅执行一次.默认情况下,timeit传递的数字"为1000000.难怪导入一个模块要花费几秒钟几百万次.

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.

话虽如此,但python的导入通常速度较慢,您会发现一些有关如何在stackoverflow和其他在线资源上加快导入(延迟导入,预编译模块等)的好建议.

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.

这篇关于Python导入非常慢-Anaconda python 2.7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆