Reload a Module in Python 3.4 [duplicate]

给你一囗甜甜゛ 提交于 2019-11-30 13:43:11

问题


I know this might sound like a really stupid question but whatever. I've made a small script in Python and I've made some changes while in a shell. Normally, on an OS X computer (It's running Python 2.7), I would simply type in reload(the_module) and it would reload my module that includes the changes that I have made. However, when I am reloading the module here (on windows python v. 3.4), it simply gives me this:

>>> reload(instfile)
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    reload(instfile)
NameError: name 'reload' is not defined

And then when I type in imp.reload(my_module), it simply says that the function is deprecated. I can't seem to find what the new function (or it's equivalent) would be anywhere so if someone can help me that would be great! :)


回答1:


The imp module was deprecated in Python 3.4 in favor of the importlib module. From the documentaion for the imp module:

Deprecated since version 3.4: The imp package is pending deprecation in favor of importlib.

So, you should be using the reload function from there:

>>> import importlib
>>> importlib.reload
<function reload at 0x01BA4030>
>>> importlib.reload(the_module)


来源:https://stackoverflow.com/questions/27387786/reload-a-module-in-python-3-4

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