1. C:\Python25\Lib\site-packages\ に次のテキストファイル sitecustomize.py を作る。
    import sys
    sys.setdefaultencoding('cp932')
    
    テキストの改行コードは Windows 標準の \r\n でも Unix 形式の \n でもよい。 このファイルが Python に認識されているかどうかは,Python の実行後, sitecustomize.py と同じ場所に sitecustomize.pyc が自動生成されているかどうかで確認できる。

    sys.setdefaultencoding 関数は画面入出力以外での Unicode の変換方法を指定する。 たとえば,画面出力をリダイレクトする場合も含め, Unicode 文字列をファイルに出力するときの Unicode からバイト文字列への デフォルトの変換方法を決める。 一方,画面入出力での変換方法は Python が OS から情報を取得して自動的に決定する。 日本語 Windows では 'cp932', つまり Shift_JIS の Windows 版バリアントになる (cf. sys.stdout.encoding, sys.stdin.encoding)。 上記で sys.setdefaultencoding 関数に 'cp932' を与えているのは, 画面入出力との振舞の一貫性を保つためである。 関数 sys.setdefaultencoding は,Python の起動スクリプト C:\Python25\Lib\site.py により sitecustomize.py の実行後,削除される。 sitecustomize.py はユーザが同関数を正当な手段で呼び出せる唯一の場所である。
    An Introduction to Python & IronPython (via -nobby-)

    2 years ago  /  Notes