Import python modules in the background in REPL

◇◆丶佛笑我妖孽 提交于 2019-12-07 13:23:13

问题


Some python modules, notably matplotlib, take a long time to load

start = datetime.datetime.now(); import numpy, pandas, matplotlib, sklearn; datetime.datetime.now() - start

takes half a second with cached files, and several seconds for non-cached files. Is there a way to load these modules in the background, when in the Python interpreter?


回答1:


You can import modules in separate threads. Here is the solution.

Create a file load_modules.py:

from concurrent.futures import ThreadPoolExecutor
import importlib
import sys

modules_to_load = ['numpy', 'pandas', 'matplotlib']


def do_import(module_name):
    thismodule = sys.modules[__name__]

    module = importlib.import_module(module_name)
    setattr(thismodule, module_name, module)
    print(module_name, 'imported')


executor = ThreadPoolExecutor()
for module_name in modules_to_load:
    executor.submit(do_import, module_name)

Then you can start interpreter with a command:

python -ic "exec(open(\"load_modules.py\").read(), globals())"

Or just run

exec(open("load_modules.py").read(), globals())

in your interpreter to load modules.



来源:https://stackoverflow.com/questions/47816669/import-python-modules-in-the-background-in-repl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!