重新加载Python模块
问题描述:
我遇到以下问题:我从GitHub(textstat)中安装了一个包含txt文件的Python包。 txt文件的内容(实际上只是一长串简单的英文单词)用于包中定义的某些功能。 现在,我通过用文本编辑器打开文件并保存它,在txt文件的列表中添加了更多的单词,但不知何故,当我执行我的python代码(在Jupyter Notebook中)时,似乎使用了旧列表,不是更新的那个。 我该如何解决这个问题?重新加载Python模块
编辑:一些更多的信息,因为reload()没有解决我的问题。还重新启动内核甚至整个计算机都没有工作......
在textstat.py中,txt文件“easy_words.txt”(与textstat.py位于同一目录中)保存在变量“easy_word_set “以下列方式:现在
easy_word_set = set([ln.strip() for ln in pkg_resources.resource_stream('textstat', 'easy_words.txt')])
,在我的笔记本Jupyter我进口textstat像往常一样:
import textstat.textstat as ts
不知何故
ts.easy_word_set
给我更新的列表。但是当我使用例如
ts.textstat.gunning_fog(word)
旧列表被使用。
答
IPython中的自动重扩展可能是有用的:
In [1]: %load_ext autoreload
In [2]: %autoreload 2
In [3]: from foo import some_function
In [4]: some_function()
Out[4]: 42
In [5]: # open foo.py in an editor and change some_function to return 43
In [6]: some_function()
Out[6]: 43
重装(your_module) –
的可能的复制[?我如何卸载(重装)一个Python模块(http://stackoverflow.com/questions/437589/how-do-i-unload-reload-a-python-module) – mx0
似乎像textstat(或pkg_resources)正在缓存关于easy_words.txt文件的信息。如果是这样,我们需要弄清楚如何清除缓存。在Linux系统上,我可能会在此时通过strace运行程序来查看它正在读取的文件。 – Waxrat