Python reload() Function

Python 内置函数Python Built-in Functions


Description

reload()Used to reload a previously loaded module.

In Python2.x versions,reload()it is a built-in function; inPython2.x ~ Python3.3later versions it was moved toimpthe package (Python2.x can also import the imp package to use it), and after Python3.4 it was moved to theimportlibpackage.

See also:Python3 reload() Function

Syntax

Python2.x versions have it as a built-in function; you can use it directly:

reload(module)

Python2.x ~ versions before Python3.3:

import imp
imp.reload(module)

or

from imp import reload
reload(module)

Python3.4 and later versions:

import importlib
importlib.reload(module)

or

from importlib import reload
reload(module)

Parameters

  • module -- The module object.

Return Value

Returns the module object.

Examples

The following examples demonstrate the usage of reload():

Python2.x: Reload the sys module and set the default encoding to utf8

>>>import sys >>> sys.getdefaultencoding() # Current default encoding 'ascii' >>> reload(sys) # Use reload <module 'sys' (built-in)> >>> sys.setdefaultencoding('utf8') # Set encoding >>> sys.getdefaultencoding() 'utf8' >>>

Python2.x~Python3.3: Reload the sys module

>>> import sys, imp
>>> imp.reload(sys)
<module 'sys' (built-in)>

>=Python3.4: Reload the sys module

>>> import sys, importlib
>>> importlib.reload(sys)
<module 'sys' (built-in)>

Python 内置函数Python Built-in Functions

Other Extensions