# Travis CI python 2.7.15 unicode issue

**URL:** https://travis-ci.community/t/travis-ci-python-2-7-15-unicode-issue/7466
**Category:** Environments
**Tags:** python, bug
**Created:** [March 6, 2020, 4:13am UTC](https://travis-ci.community/t/travis-ci-python-2-7-15-unicode-issue/7466 "2020-03-06T04:13:32Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![kirankotari](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/kirankotari/32/3208_2.png) [@kirankotari](https://travis-ci.community/u/kirankotari)
#### Post date: [March 6, 2020, 4:13am UTC](https://travis-ci.community/t/travis-ci-python-2-7-15-unicode-issue/7466/1 "2020-03-06T04:13:32Z")

</div>

When I have run the pytest in my local with 2.7.15 it works fine but in Travis CI it fails to say ‘UnicodeEncodeError’

```
    $ PYTHONPATH=. pytest -v -s --cov=pingping tests
============================= test session starts ==============================
platform linux2 -- Python 2.7.15, pytest-4.6.6, py-1.7.0, pluggy-0.13.1 -- /home/travis/virtualenv/python2.7.15/bin/python
cachedir: .pytest_cache
rootdir: /home/travis/build/network-tools/pingping
plugins: cov-2.8.1
collecting ... INFO:root:Logging Time : 2020-03-06 03:30:34.992237
collected 6 items

tests/test_ping.py::TestPing::test_ping_en PASSED
tests/test_ping.py::TestPing::test_ping_spanish FAILED
tests/test_ping.py::TestPing::test_ping_french FAILED
tests/test_ping.py::TestPing::test_ping_afrikaans PASSED
tests/test_ping.py::TestPing::test_ping_telugu FAILED
tests/test_ping.py::TestPing::test_ping_hindi FAILED

=================================== FAILURES ===================================
__________________________TestPing.test_ping_spanish__________________________

self = <tests.test_ping.TestPing instance at 0x7fb04e652d40>
setup = <pingping.ping.Ping instance at 0x7fb04e652b00>

 def test_ping_spanish(self, setup):
> result = self.read_all_inputs(setup, langauge='spanish')

tests/test_ping.py:88:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
tests/test_ping.py:23: in read_all_inputs
 result[os][ip] = obj.fetch_ping_data(each_ping)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

cls = <class pingping.ping.Ping at 0x7fb04e48d0b8>
data = 'Haciendo ping a 1.1.1.1 con 32 bytes de datos:
Responder desde 1.1.1.1: bytes = 32 ms = 87ms TTL = 127
Responder de... (0% de pérdida),
Tiempo aproximado de ida y vuelta en milisegundos:
 Mínimo = 68ms, máximo = 99ms, promedio = 81ms '
command = 'ping', loss_percentage_filter = 75

 @classmethod
 def fetch_ping_data(cls, data, command='ping', loss_percentage_filter=75):
 if type(data) is not list:
> data = str(data).split('\n') # .encode('utf-8') issue in 2.7
E UnicodeEncodeError: 'ascii' codec can't encode character u'\xed' in position 281: ordinal not in range(128)

pingping/ping.py:118: UnicodeEncodeError
__________________________TestPing.test_ping_french___________________________

```

[https://travis-ci.org/network-tools/pingping](https://travis-ci.org/network-tools/pingping) Build 57

---

<div class="post-metadata">

### Author: ![native-api](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/native-api/32/430_2.png) [@native-api](https://travis-ci.community/u/native-api)
#### Post date: [March 6, 2020, 4:17am UTC](https://travis-ci.community/t/travis-ci-python-2-7-15-unicode-issue/7466/2 "2020-03-06T04:17:08Z")

</div>

> <https://stackoverflow.com/questions/49941052/which-encoding-is-used-for-strings-in-python-2-x>

---

<div class="post-metadata">

### Author: ![kirankotari](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/kirankotari/32/3208_2.png) [@kirankotari](https://travis-ci.community/u/kirankotari)
#### Post date: [March 6, 2020, 4:52am UTC](https://travis-ci.community/t/travis-ci-python-2-7-15-unicode-issue/7466/3 "2020-03-06T04:52:58Z")

</div>

Thanks, I understood the problem, here is the solution: [https://stackoverflow.com/questions/2276200/changing-default-encoding-of-python](https://stackoverflow.com/questions/2276200/changing-default-encoding-of-python)

I have written the following code in my test class.

```
import sys        
if sys.version_info.major == 2:
    import imp
    imp.reload(sys)
    sys.setdefaultencoding('utf-8')
```

---

<div class="post-metadata">

### Author: ![native-api](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/native-api/32/430_2.png) [@native-api](https://travis-ci.community/u/native-api)
#### Post date: [March 6, 2020, 4:54am UTC](https://travis-ci.community/t/travis-ci-python-2-7-15-unicode-issue/7466/4 "2020-03-06T04:54:03Z")

</div>

I wouldn’t call it a “solution” (at least, not unless you understand its problems and sign up for dealing with them):

> <https://stackoverflow.com/questions/3828723/why-should-we-not-use-sys-setdefaultencodingutf-8-in-a-py-script>

The real solution is to always convert `str<->unicode` explicitly, with explicit encoding. For I/O, you can set `PYTHONIOENCODING` but then you need to somehow make sure it’s always set.

---

<div class="post-metadata">

### Author: ![kirankotari](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/kirankotari/32/3208_2.png) [@kirankotari](https://travis-ci.community/u/kirankotari)
#### Post date: [March 6, 2020, 5:05am UTC](https://travis-ci.community/t/travis-ci-python-2-7-15-unicode-issue/7466/5 "2020-03-06T05:05:07Z")

</div>

You are right, but setting `PYTHONIOENCODING` is not solving the defaultencoding problem.

---

<div class="post-metadata">

### Author: ![linehammer](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/linehammer/32/6012_2.png) [@linehammer](https://travis-ci.community/u/linehammer)
#### Post date: [April 12, 2021, 7:07am UTC](https://travis-ci.community/t/travis-ci-python-2-7-15-unicode-issue/7466/6 "2021-04-12T07:07:35Z")

</div>

> [@kirankotari](#):
>
> `UnicodeEncodeError: 'ascii' codec can't encode character `

Error indicates an unicode character cannot be ascii-encoded.

To use a specific encoding (e.g. ‘utf-8’) use the encoding argument for [pandas to csv](http://net-informations.com/ds/pd/tocsv.htm):

`df.to_csv(file_name, sep='\t', encoding='utf-8')`
