the5fire

关注Python、Django、Vim、Linux、Web开发、团队管理和互联网--Life is short, we need Python.


为什么需要reload(sys)后设置编码--python中的site.py模块

作者:the5fire | 标签:       | 发布:2014-08-28 6:35 a.m. | 阅读量: 13615, 12890

在刚开始写python程序的时候,都会遇到一个很头疼的问题——编码错误,在之前的文章中也做了介绍: future中unicode_literals引起的错误来研究python中的编码问题 。其中一种解决方案是reload(sys),然后调用sys.setdefaultencoding('utf-8'),之前只是知道这么做能够设置运行时编码,那么为什么要reload之后才能设置呢?

最近在看newrelic的python client,在研究它是如何获取进程运行的状态。看到了它对site.py这个模块的使用,这个模块的介绍看这里:https://docs.python.org/2/library/site.html ,这个模块的介绍是:Site-specific configuration hook, 特定的site配置hook。newrelic中当然没用用到reload(sys)这样的东西,它只是用了sitecustomize这个东西,这是用来对site-packages在加载时可以自定义hook的东西。newrelic用到了这个东西,然后我查看了site.py的源码,发现这个代码, http://hg.python.org/cpython/file/2.7/Lib/site.py#l545::

# Remove sys.setdefaultencoding() so that users cannot change the
# encoding after initialization.  The test for presence is needed when
# this module is run as a script, because this code is executed twice.
if hasattr(sys, "setdefaultencoding"):
    del sys.setdefaultencoding

因此,如果需要在运行时(就是整个解释器启动完之后)进行setdefaultencoding,就只能重新加载sys这个模块。

最后补充一下,site.py这个模块还有一个很重要的说明:This module is automatically imported during initialization. 这个模块会在解释器启动的时候自动执行,作用是加载site-packages中的包和模块到python的sys.path里面,让你可以在代码中import你安装的包。

- from the5fire.com
----EOF-----

微信公众号:Python程序员杂谈


其他分类: