Python3 - 无法在 __import__ 对象上调用 reload()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18249459/
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 10:17:45  来源:igfitidea点击:

Python3 - reload() can not be called on __import__ object?

pythonpython-3.xmodulereloadpython-3.3

提问by Torxed

Ok so for a number of reasons, I've been using s = __import__('parse')for the longest time in Python2, now I sorta need to being my transitions to Python3 for certain projects (mainly due to SSL).

好的,出于多种原因,我s = __import__('parse')在 Python2 中使用的时间最长,现在我有点需要为某些项目转换到 Python3(主要是由于 SSL)。

But there's no reload()equivilant in Python3 as far as i've spotted. There's only one option:

但是就reload()我所见,Python3 中没有等效项。只有一种选择:

import parse
parse.reload() #works

But I really, really need this to work:

但我真的,真的需要这个来工作:

parse = __import__('parse')
parse.reload()

or

或者

reload(parse)

Any ideas on how to get it working in Python3?

关于如何让它在 Python3 中工作的任何想法?

采纳答案by Ashwini Chaudhary

The reloadbuilt-in function has been moved to importlibmodule in Python 3.4:

reload内置功能已被转移到importlib模块在Python 3.4:

In [18]: from importlib import reload

In [19]: reload?
Reload the module and return it.

The module must have been successfully imported before.

As pointed out by @JPaget in comments reload()function has been moved from impto importlibmodule in Python 3.4+. From what's new in Python 3.4:

正如评论所指出的@JPagetreload()功能已经从被移动impimportlib模块中的Python 3.4+。从Python 3.4 中的新内容

The reload()function has been moved from impto importlibas part of the impmodule deprecation

reload()功能已经从移动impimportlib作为一部分imp模块弃用