Python solves the problem of NameError: name ‘reload’ is not defined

Encountered this problem, for Python 2.X:

1 import sys
2 reload(sys)
3 sys.setdefaultencoding( " utf-8 " )

For Python 3.3 and below:
1 import imp
2 imp.reload(sys)

requires attention:
1. There is a big difference between Python 3 and Python 2. The Python 3 system uses utf- 8 encoding by default .
2. So, for the case of using Python 3, the code of sys.setdefaultencoding( ” utf-8 ” ) is not needed.
3. The most important thing is that there is no setdefaultencoding() function in Python 3 sys library.

For Python 3.4 and above:
1 import importlib
2 importlib.reload(sys)

Similar Posts: