python之reload用法

一、python2和python3的区别

python2中可以直接使用reload()。python3中需要从库中导入,有两种方法:

>>> from imp import reload
>>> reload(moudle)

import imp
imp.reload(moudle)

二、python3中reload()中出现错误的原因

>>> import os
>>> os.getcwd()
'C:\\CodingApp\\python'
>>> os.chdir(r'C:\Users\JIANCHEN\PycharmProjects\决策树\src')
>>> os.getcwd()
'C:\\Users\\JIANCHEN\\PycharmProjects\\决策树\\src'
>>> from imp import reload

Warning (from warnings module):
  File "__main__", line 1
DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
>>> reload(index)
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    reload(index)
NameError: name 'index' is not defined
>>> import index
>>> reload(index)
<module 'index' from 'C:\\Users\\JIANCHEN\\PycharmProjects\\决策树\\src\\index.py'>

总结:在reload之前要import

posted @ 2019-04-07 20:19  twoint  阅读(14441)  评论(0编辑  收藏  举报