Python reload() Function
Description
reload() Used to reload a module that was previously loaded.
In Python 2.x versions reload() is a built-in function, in Python2.x ~ Python3.3 it was moved in version imp package (Python 2.x could also import the imp package); after Python 3.4 it was moved to the importlib package.
See also:Python3 reload() Function
Syntax
In Python 2.x it is a built-in function and can be used directly:
reload(module)
Python 2.x ~ versions before Python 3.3:
import imp
imp.reload(module)
or
from imp import reload
reload(module)
Versions after Python 3.4:
import importlib
importlib.reload(module)
or
from importlib import reload
reload(module)
Parameters
- module -- the module object.
Return Value
Returns the module object.
Example
The following examples demonstrate how to use reload():
Python 2.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
'
Python 2.x ~ Python 3.3: reload the sys module
import
sys
,
imp
imp
.
reload
(
sys
)
<
module
'sys'
(
built-
in
)
>= Python 3.4: reload the sys module
import
sys
,
importlib
importlib.
reload
(
sys
)
<
module
'sys'
(
built-
in
)
Python built-in function