在 Python 3.4 中重新加载模块 [英] Reload a Module in Python 3.4

查看:45
本文介绍了在 Python 3.4 中重新加载模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<预><代码>>>>重新加载(instfile)回溯(最近一次调用最后一次):文件<pyshell#5>",第 1 行,在 <module> 中重新加载(instfile)NameError:未定义名称重新加载"

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

解决方案

imp 模块在 Python 3.4 中被弃用,取而代之的是 importlib 模块.来自 imp 模块的文档:><块引用>

自 3.4 版起已弃用:imp 包正在等待弃用支持 importlib.

所以,你应该从那里使用 reload 函数:

<预><代码>>>>导入导入库>>>importlib.reload<函数在 0x01BA4030 处重新加载>>>>importlib.reload(the_module)

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

解决方案

The imp module was deprecated in Python 3.4 in favor of the importlib module. From the documentation 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)

这篇关于在 Python 3.4 中重新加载模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆