UnicodeEncodeError: 'ascii' codec can't encode characters in position 26-27: ordinal not in range(128)
<p>I'm running a job that runs a python script collecting Facebook events data from Facebook Graph API and printing out information about the events using Heroku Scheduler. Since it is an automated job that runs daily at a specific time in the cloud, the printing to screen will be logged into Heroku logs when that job starts running in the cloud by Heroku Scheduler.</p>

<p>The weird thing is, I have no problem when I run the python script manually by executing a heroku run command, but when it is run by the Heroku Scheduler automatically in the cloud and logging the printing, there is an encoding error.</p>

<p>The relevant python script with the error is as follows:</p>

<pre><code>def importFromPages():
    sources = sources_collection.find(
        projection= {
            'id'        : 1,
            'source'    : 1,
            'name'      : 1,
            '_id'       : 0
        }
    )

    headers = {
        'Content-Type'  : 'application/json'
    }

    params = {
        'fields'        : 'id, name, cover, start_time, end_time, place',
        'access_token'  : FB_ACCESS_TOKEN
    }

    for source in sources:
        print(source['name'])
        data = requests.get(
            'https://graph.facebook.com/v2.7/{}/events'.format(source['id']),
            params=params,
            headers=headers
        )

        if data.status_code == 200:
            data = json.loads(data.text)
            data = data['data']
            if len(data) == 0: continue

            for event in data:
                now     = arrow.now().timestamp
                end     = arrow.get(event.get('end_time', None)).timestamp

                if end != None:
                    if end &lt;= now: continue

                print(event['id'])
                event['host'] = source
                events_collection.update(
                    { 'id'      : event['id'] },
                    { '$set'    : event },
                    upsert=True
                )
        else:
            print('data.status_code != 200')
</code></pre>

<p>The heroku logs that showed the encoding error when the job is scheduled to run is here:</p>

<pre><code>2016-10-18T12:31:34.270891+00:00 app[scheduler.1368]: UNSW Bitfwd
2016-10-18T12:31:34.411757+00:00 app[scheduler.1368]: IGA UNSW
2016-10-18T12:31:34.531127+00:00 app[scheduler.1368]: UNSW Barbell Club
2016-10-18T12:31:34.721310+00:00 app[scheduler.1368]: 660525034113560
2016-10-18T12:31:34.724900+00:00 app[scheduler.1368]: Circusoc - The UNSW Circus Society
2016-10-18T12:31:34.947553+00:00 app[scheduler.1368]: UNSW Medical Music Society
2016-10-18T12:31:35.140233+00:00 app[scheduler.1368]: 1166082866820866
2016-10-18T12:31:35.145683+00:00 app[scheduler.1368]: MODsoc Ministry of Dance UNSW
2016-10-18T12:31:35.370460+00:00 app[scheduler.1368]: Arc at UNSW Art &amp; Design
2016-10-18T12:31:35.616661+00:00 app[scheduler.1368]: 1145482732204348
2016-10-18T12:31:35.622455+00:00 app[scheduler.1368]: Accounting Society Of UNSW (AccSoc)
2016-10-18T12:31:35.815662+00:00 app[scheduler.1368]: 1375370362476567
2016-10-18T12:31:35.817958+00:00 app[scheduler.1368]: 1679765869019301
2016-10-18T12:31:35.821599+00:00 app[scheduler.1368]: Traceback (most recent call last):
2016-10-18T12:31:35.821601+00:00 app[scheduler.1368]:   File "data.py", line 127, in &lt;module&gt;
2016-10-18T12:31:35.821652+00:00 app[scheduler.1368]:     main()
2016-10-18T12:31:35.821655+00:00 app[scheduler.1368]:   File "data.py", line 124, in main
2016-10-18T12:31:35.821709+00:00 app[scheduler.1368]:     importFromPages()
2016-10-18T12:31:35.821711+00:00 app[scheduler.1368]:   File "data.py", line 92, in importFromPages
2016-10-18T12:31:35.821715+00:00 app[scheduler.1368]:     print(source['name'])
2016-10-18T12:31:35.821745+00:00 app[scheduler.1368]: UnicodeEncodeError: 'ascii' codec can't encode characters in position 26-27: ordinal not in range(128)
2016-10-18T12:31:36.333406+00:00 heroku[scheduler.1368]: Process exited with status 1
2016-10-18T12:31:36.337350+00:00 heroku[scheduler.1368]: State changed from up to complete
</code></pre>

<p>When I run the job manually using heroku run as follows, there is no encoding print error and the job is able to complete without any errors:</p>

<pre><code>heroku run python data.py
Running python data.py on â¬¢ eventobotdatacollector... up, run.4056 (Free)
</code></pre>

<p>Does anyone have an idea what is the problem here and why the different environments caused the error?</p>

<p>I don't know about the influences of the different environments. But I was able to solve a similar problem by explicity unicode-encoding the data I wanted to print out (or write to a file, for that matter). Try</p>

<pre><code>print(source['name'].encode('utf-8'))
</code></pre>

<p>or have a look here, for a more complete and knowledgeable explanation of unicode issues in Python: <a href="http://stackoverflow.com/questions/2392732/sqlite-python-unicode-and-non-utf-data">SQLite, python, unicode, and non-utf data</a></p>

<p>Hope this helps. Regards,</p>

python facebook heroku encoding