2012-05-01 4 views
12

を使用して、私は次のようなエラーが奇妙な取得:私は、ファイル名のrepr()を含めるは `UnicodeEncodeError`は(フラスコを使用して)Webアプリケーションで` os.path.exists`

Unable to retrieve the thumbnail for u'/var/data/uploads/2012/03/22/12 Gerd\xb4s Banjo Trio 1024.jpg' 
Traceback (most recent call last): 
File "/var/www/beta/env/lib/python2.7/site-packages/dblib-1.0dev3-py2.7.egg/dblib/orm/file.py", line 169, in get_thumbnail 
    if not exists(filename): 
File "/usr/lib/python2.7/genericpath.py", line 18, in exists 
    os.stat(path) 
UnicodeEncodeError: 'ascii' codec can't encode character u'\xb4' in position 52: ordinal not in range(128) 

注意ログに記録されたエラー。これは、ファイル名がUnicodeインスタンスとして渡されることを示しています。フラスコ環境で実行されている、Pythonはそれがファイルをエンコードする必要があります考えている間、

>>> from os.path import exists 
>>> exists(u'/var/data/uploads/2012/03/22/12 Gerd\xb4s Banjo Trio 1024.jpg') 
True 

だから、明らかに:あまりにも多く、私はPythonインタプリタを使用して犯人を実行する場合、期待どおり、それが動作する...

正しいですか-nameは、UTF-8ではなくASCIIコーデックを使用します。 Apache httpdの後ろにmod_wsgiを使ってアプリケーションをデプロイしました。

どこかの人にUTF-8を使用するように指示しなければならないと思いますか?しかしここで?

+0

が、これは適切な解決策だとは思わないが、同様の状況で、私が見つけたときに自分が使用して'string'.encode(' utf8 ')はトリックを行います。ショットに値する可能性があります。 –

+0

設定の問題だと思います。だから、私が正しい設定を見つけ出すまで、私はそれが起こるたびに電子メールで私に腹を立てるtry/exceptブロックでそれを守っただけです:Pそれはエラーです。それを思い出してほしいです:) – exhuma

答えて

17

同じ問題については、Djangoのドキュメントを参照してください。上記のリンクドキュメントから

https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/modwsgi/#if-you-get-a-unicodeencodeerror

抜粋:のmod_wsgiを使用する場合は、同じ溶液である必要があり

[...] you must ensure that the environment used to start Apache is configured to accept non-ASCII file names. If your environment is not correctly configured, you will trigger UnicodeEncodeError exceptions when calling functions like the ones in os.path on filenames that contain non-ASCII characters.

To avoid these problems, the environment used to start Apache should contain settings analogous to the following:

export LANG='en_US.UTF-8' 
export LC_ALL='en_US.UTF-8' 

Consult the documentation for your operating system for the appropriate syntax and location to put these configuration items; /etc/apache2/envvars is a common location on Unix platforms. Once you have added these statements to your environment, restart Apache.

+4

ちょっとメモ:そのURLにもう参照していないようです。代わりに私はここでそれを見つけました:https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/modwsgi/#if-you-get-a-unicodeencodeerror –

+3

'uwsgi.ini'には引用符: 'env = LANG = en_US.UTF-8' –

+0

私の場合、' uwsgi.ini'に 'env = LANG = en_US.UTF-8'をhttps://docs.djangoproject.com/enに追加しました/ dev/howto/deployment/wsgi/uwsgi /#s-configuration-and-the-uwsgi-server-for-django – shellbye

関連する問題