Import python modules in the background in REPL

。_饼干妹妹 提交于 2019-12-06 00:06:31

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.

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