Instapy: UnicodeEncodeError: ordinal not in range(128)

Created on 22 Apr 2017  路  9Comments  路  Source: timgrossmann/InstaPy

Seems like emoji aren鈥檛 working on my VPS:

File "/home/tibor/InstaPy/instapy/instapy.py", line 306, in like_by_tags
    commented += comment_image(self.browser, comments)
  File "/home/tibor/InstaPy/instapy/comment_util.py", line 17, in comment_image
    print(u'--> Commented: {}'.format(rand_comment))
UnicodeEncodeError: 'ascii' codec can't encode character u'\u270c' in position 23: ordinal not in range(128)

Does anyone has an idea why this isn鈥檛 working?

On the Python FAQ I found this quote:

These messages usually means that you鈥檙e trying to either mix Unicode strings with 8-bit strings, or is trying to write Unicode strings to an output file or device that only handles ASCII.
When you do this, Python will usually assume that the 8-bit string contains ASCII data only, and will raise an error if this is not the case.

bug help wanted

Most helpful comment

Just add these lines to quickstart.py and instapy.py:

# -*- coding: UTF-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

All 9 comments

Sorry, found the answer myself. I guess we should think about a small FAQ in addition to the quickstart guide.

@tibor could you please quickly write what the answer was? (For future issues)

I thought it would work with the solution from hariom282538 from #45:

set PYTHONIOENCODING=UTF-8

Unfortunately, chrome crashes then.

At the moment, my server setup is as following:

$ locale
LANG=C
LC_CTYPE="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_COLLATE="C"
LC_MONETARY="C"
LC_MESSAGES="C"
LC_PAPER="C"
LC_NAME="C"
LC_ADDRESS="C"
LC_TELEPHONE="C"
LC_MEASUREMENT="C"
LC_IDENTIFICATION="C"
LC_ALL=

Would I have to change anything here?

@tibor LC_ALLshould definitely be en_US.UTF-8

Now it seems to work.
My steps were:

$ localedef -i en_US -f UTF-8 en_US.UTF-8
$ export LC_ALL=en_US.UTF-8

@tibor Perfect, thank you for the reply!

Just add these lines to quickstart.py and instapy.py:

# -*- coding: UTF-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

@hojjabr this is a great solution - thanks :)

Was this page helpful?
0 / 5 - 0 ratings