Python设置编码和PYTHONPATH
?
Python中的编码是个恼人的问题,第一个是文件编码,在第一行设置了#-*- coding: utf-8 -*-就可以解决。
第二个是环境编码,就是你有个中文unicode的encode或decode操作,它给你报错。
我们最不喜欢看见这段出错信息了:
# -*- coding: utf-8 -*-import os.pathimport sysimport sitetry: reload(sys) sys.setdefaultencoding("utf-8")except AttributeError: passbase_dir = os.path.dirname(os.path.abspath(__file__))prev_sys_path = list(sys.path)# site.addsitedir adds this directory to sys.path then scans for .pth files# and adds them to the path too.site.addsitedir(os.path.join(base_dir, 'library'))# addsitedir adds its directories at the end, but we want our local stuff# to take precedence over system-installed packages.# See http://code.google.com/p/modwsgi/issues/detail?id=112new_sys_path = []for item in list(sys.path): if item not in prev_sys_path: new_sys_path.append(item) sys.path.remove(item)sys.path[:0] = new_sys_path