3

If I connect to a Heroku worker dyno with heroku run (e.g. heroku run python for an interactive Python session), any attempt to display Unicode characters via this results in a UnicodeEncodeError

Locally:

$ python
Python 2.7.1 (r271:86832, Jun 25 2011, 05:09:01) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print u'\xa3'
£

Via heroku run:

$ heroku run python
Running python attached to terminal... up, run.1
Python 2.7.2 (default, Oct 31 2011, 16:22:04) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print u'\xa3'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa3' in position 0: ordinal not in range(128)
>>>

Now, if I heroku run bash and use echo to try to display things, all seems fine (except my local choice of font!):

$ heroku run bash
Running bash attached to terminal... up, run.2
~ $ echo -e "\xa3"
?

I assume I'm doing something wrong / missing something, but somewhat lost as to what, or further lines of investigation.

3 Answers 3

8

I believe this will fix your problem:

$ heroku config:set LANG=en_US.UTF-8

or, if after you try to run that, heroku complains that you need to supply the app parameter:

$ heroku config:set LANG=en_US.UTF-8 -a <app_name>
Sign up to request clarification or add additional context in comments.

2 Comments

This is default on all new Heroku Python apps now :)
Oh, as a function of the buildpack?
3

I had similar issues. The problem appears to be that, when it is not outputting to a terminal, Python 2 chooses ascii as the default encoding for the print statement. There are ways around this, including the complex but accurate method described here. There are similar methods scattered around the net.

However, there is an easier, but deprecated, solution. This happens to be the one I use, which consists of inserting this at the beginning of your program:

reload(sys) 
sys.setdefaultencoding("utf-8")

This forces print to use the utf-8 default encoding rather than ascii, and should, hopefully, fix your problem.

2 Comments

Alas the solution you describe doesn't seem to work for me, but the link seems highly relevant, thanks!
It may not work in the interpreter - I am away from my system and not able to check - but it ought to work if you try it in a script?
3

None of the above worked but this did:

heroku config:add PYTHONIOENCODING=utf8

This is one of the answers to this question here:

UnicodeEncodeError only when running as a cron job

unfortunately it wasn't the accepted answer and that question is marked as a dup so I'm reposting here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.