2012-05-01 27 views
12

Trong một ứng dụng web (sử dụng Flask), tôi nhận được lỗi sau:Strange 'UnicodeEncodeError` sử dụng `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) 

Lưu ý rằng tôi bao gồm các repr() của tên tập tin trong lỗi đăng nhập. Điều này cho thấy tên tập tin được truyền như một thể hiện Unicode. Vì vậy, nhiều là đúng ...

Nếu tôi chạy thủ phạm sử dụng thông dịch viên python, nó hoạt động như mong đợi:

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

Vì vậy, rõ ràng, trong khi chạy trong môi trường Flask, Python nghĩ rằng nó nên mã hóa các tập tin -name bằng cách sử dụng codec ASCII thay vì UTF-8. Tôi đã triển khai ứng dụng bằng cách sử dụng mod_wsgi phía sau Apache httpd.

Tôi cho rằng mình phải nói một trong hai cách sử dụng UTF-8 ở đâu đó? Nhưng ở đâu?

+0

Đừng nghĩ rằng đây là giải pháp thích hợp, nhưng khi trong những tình huống tương tự tôi thấy mình sử dụng 'string'.encode (' utf8 ') thực hiện thủ thuật - có thể đáng để bắn. –

+0

Tôi chắc chắn đó là sự cố cấu hình. Vì vậy, cho đến khi tôi tìm ra các thiết lập đúng, tôi chỉ đơn giản là bảo vệ nó với một khối try/except peppering tôi với email mỗi khi nó xảy ra: P Đó là một lỗi và tôi muốn được nhắc nhở về nó :) – exhuma

Trả lời

17

Xem tài liệu Django cho cùng một vấn đề. Khi sử dụng mod_wsgi, nên cùng một giải pháp:

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

Trích từ doc liên kết ở trên:

[...] 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

Chỉ cần lưu ý: neo bạn đang đề cập đến dường như không tồn tại trong URL đó nữa. Thay vào đó tôi tìm thấy nó ở đây: https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/modwsgi/#if-you-get-a-unicodeencodeerror –

+3

Trong 'uwsgi.ini' nó không được chứa dấu ngoặc kép : 'env = LANG = en_US.UTF-8' –

+0

Trong trường hợp của tôi, tôi đã thêm' env = LANG = en_US.UTF-8' vào 'uwsgi.ini', theo https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/uwsgi/# s-configuring-and-starting-the-uwsgi-server-cho-django – shellbye