Python3 reload() Function
Description
reload() Used to reload a module that was previously loaded.
In Python 2.x versions reload() it was a built-in function and could be used directly; see Python2.x reload() 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.
Syntax
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 example shows how to use reload().
Example 1
Reload the sys module
import
sys
,
importlib
importlib.
reload
(
sys
)
<
module
'sys'
(
built-
in
)
Example 2
First, create a runoob.py file in the current directory:
The runoob.py file
Test code for the runoob.py file
site
=
"
RUNOOB
"
Start a Python interactive command window in the current directory:
Example
import
runoob
runoob
.
site
'
RUNOOB
'
Then edit the runoob.py file in another window (do not close the Python interactive window above), changing it to the following code:
The modified runoob.py file
Test code for the runoob.py file
site
=
"
"
Then return to the Python interactive command window:
Example
runoob.
site
The output result has not changed
'RUNOOB'
from
importlib
import
reload
Python 3.4+
reload
(
runoob
)
Reload the modified runoob.py file
<
module
'runoob'
from
'/Users/RUNOOB/runoob-test/runoob.py'
runoob.
site
The output result is now correct
'GOOGLE'
Python built-in function