在 Python 3.4 中重新加载模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27387786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:45:09  来源:igfitidea点击:

Reload a Module in Python 3.4

pythonpython-3.xpython-3.4python-module

提问by Zizouz212

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:

我知道这听起来可能是一个非常愚蠢的问题,但无论如何。我用 Python 编写了一个小脚本,并在 shell 中进行了一些更改。通常,在 OS X 计算机(它运行 Python 2.7)上,我只需输入reload(the_module)并重新加载我的模块,其中包括我所做的更改。但是,当我在这里重新加载模块时(在 windows python v. 3.4 上),它只是给了我这个:

>>> 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! :)

然后当我输入时imp.reload(my_module),它只是说该函数已被弃用。我似乎无法在任何地方找到新功能(或等效功能)的内容,所以如果有人可以帮助我那就太好了!:)

采纳答案by Zizouz212

The impmodule was deprecated in Python 3.4 in favor of the importlibmodule. From the documentationfor the impmodule:

imp模块在 Python 3.4 中被弃用,取而代之的是importlibmodule。从模块的文档imp

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

自 3.4 版后已弃用:该imp软件包正在等待弃用,以支持importlib.

So, you should be using the reloadfunction from there:

因此,您应该reload从那里使用该函数:

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