重构代码以将iPython中的大型对象/模型保留在内存中,以便在python脚本中重用

2024-09-27 00:17:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我的脚本依赖于在一分钟内加载大量变量,并在许多函数中全局使用它们。每次我在iPython中调用这个脚本,它都会再次加载它们,这需要时间

我试图通过这些调用从脚本中加载和填充函数,但是这些全局变量对脚本中的函数不可用。 它给出NameError: name 'clf' is not defined错误消息

有没有最好的方法来重构这些代码,以便将这些全局变量保存在内存中,并让脚本使用它们?脚本加载了许多这样的变量,并将它们作为全局变量在其他函数中使用

vectorizer_title, vectorizer_desc, clf, df_instance, vocab, all_tokens, df_dist_all, df_soc2class_proba, dict_p2s, dict_f2m, token_pattern, cleanup_pattern, excluded_words = load_data_and_model(lang) dict_token2idx_all, dict_token2idx_instance, dist_array, token_dist_to_instance_min, dict_bigram_by_instance, denominate, similar_threshold = populate_data(1)


Tags: instance函数脚本tokendfdatadistall
1条回答
网友
1楼 · 发布于 2024-09-27 00:17:36

我试过之后问了这个问题 from depended_library import * 它在伊皮顿不起作用。 但与python一起使用,并在flaskwebapi中使用,它可以工作

使用“from”语句导入库除了定义函数外,还执行库中函数的代码

(如果有人用iPython解释问题并提出解决方案,我会选择它作为答案。)

相关问题 更多 >

    热门问题