かのとの付き合い方"について考える利用Unicodeデータをsetup.py?
-
18-09-2019 - |
質問
私は書くsetup.py のためのPythonパッケージを使用setuptoolsために非ASCII文字のlong_description分野:
#!/usr/bin/env python
from setuptools import setup
setup(...
long_description=u"...", # in real code this value is read from a text file
...)
残念ながら、unicodeオブジェクトにsetup()では初の試みとし、以下いずれかのコマンドとUnicodeEncodeError
python setup.py --long-description | rst2html python setup.py upload
を利用した場合、原UTF-8文字列のlong_description分野、以下のコマンドの休憩をUnicodeDecodeError:
python setup.py register
私は一般的にリリースソフトウェアを実行'python setup.py sdist登録をアップロード'という醜hacksることを見sys.argvには、権オブジェクトタイプは右ます。
最終的に作った、実施して異なる醜いhack:
class UltraMagicString(object):
# Catch-22:
# - if I return Unicode, python setup.py --long-description as well
# as python setup.py upload fail with a UnicodeEncodeError
# - if I return UTF-8 string, python setup.py sdist register
# fails with an UnicodeDecodeError
def __init__(self, value):
self.value = value
def __str__(self):
return self.value
def __unicode__(self):
return self.value.decode('UTF-8')
def __add__(self, other):
return UltraMagicString(self.value + str(other))
def split(self, *args, **kw):
return self.value.split(*args, **kw)
...
setup(...
long_description=UltraMagicString("..."),
...)
なんじゃないよ。
解決
あるdistutilsのバグが修正されpython2.6: http://mail.python.org/pipermail/distutils-sig/2009-September/013275.html
Tarekするパッチpost_to_server.パッチのは前工程で使用されるすべての値の "データ"の引数としてのunicodeを呼び出の方法です。見 http://mail.python.org/pipermail/distutils-sig/2009-September/013277.html
他のヒント
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from setuptools import setup
setup(name="fudz",
description="fudzily",
version="0.1",
long_description=u"bläh bläh".encode("UTF-8"), # in real code this value is read from a text file
py_modules=["fudz"],
author="David Fraser",
author_email="davidf@sjsoft.com",
url="http://en.wikipedia.org/wiki/Fudz",
)
私は試験上記のコードエラーがないから--長説明けからrst2html;アップロードうOKが取り止めます実際にアップロード)に登録聞く自分のユーザー名かについてお答えください。そのトレースバックにコメントのもとに自動変換 unicode
の register
コマンドを起こす問題です。
見 のillusive setdefaultencoding 詳細などについて-基本的にはそのままにしてデフォルトエンコーディングPythonでき換された符号化された文字列に戻るunicodeでトリッキーを設定します。この場合のように:
import sys
reload(sys).setdefaultencoding("UTF-8")
も正できるから locale
-あるコードのコメントアウトされている /usr/lib/python2.6/site.py
がきっと見つかるはずでこんが議論のためです。
変更する必要があるおunicode長の説明 u"bläh bläh bläh"
通常の文字列 "bläh bläh bläh"
追加エンコードヘッダーとしてのファイル:
#!/usr/bin/env python
# encoding: utf-8
...
...
明らかに、必要なものを保存するファイルをUTF-8エンコーディングです。