简体   繁体   English

为什么导入 openpyxl 需要这么长时间?

[英]Why does importing the openpyxl take so long?

I wrote a GUI program using python tkinter and noticed it's extremely slow to start up, it took 8 seconds for the window to show up!我使用 python tkinter 编写了一个 GUI 程序,发现它启动非常慢,窗口显示需要 8 秒! I checked the code and finally found it's because of an Excel read/write module named openpyxl .我检查了代码,最后发现这是因为名为openpyxl的 Excel 读/写模块。 When I commented out thie line: import openpyxl .当我注释掉这行时: import openpyxl The GUI window show up instantly. GUI 窗口立即出现。

I also tried to run code import openpyxl on python prompt and it nearly took 3 seconds to finish running the code!我还尝试在 python 提示符下运行 code import openpyxl ,几乎用了 3 秒钟来完成运行代码!

Is this normal take so long to load this module?加载这个模块需要这么长时间吗?

PS: i'm using openpyxl 3.0.2 PS:我正在使用 openpyxl 3.0.2

I don't know outright why it's so slow, however I do know that it simply is.我不完全知道为什么它这么慢,但我知道它就是这样。 You can try using a code profiler (like in PyCharm) to find out what exactly takes so long, or you can improve user experience by importing the module at runtime whenever you need it.您可以尝试使用代码分析器(如在 PyCharm 中)来找出究竟是什么需要这么长时间,或者您可以通过在运行时随时导入模块来改善用户体验。

Take the following:采取以下措施:

import openpyxl


... gui stuff


def on_button_clicked():
    openpyxl.do_something()
    ...done

The following code would take ages to load up for the user, changing it to the following would drastically improve startup performance and lead to a better user experience.以下代码需要很长时间才能为用户加载,将其更改为以下代码将大大提高启动性能并带来更好的用户体验。

Do note that at the end of the day the code would still take 3 seconds to load, but you're moving it to a place where people don't mind it to happen as they expect things to take a little longer if they press a button and a loader gets shown请注意,在一天结束时,代码仍然需要 3 秒才能加载,但是您将它移到人们不介意它发生的地方,因为他们预计如果按下按钮并显示加载程序


... gui stuff


def on_button_click():
    ... show a loader or something
    import openpyxl
    openpyxl.do_something()
    ... done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM