# Python3 and iohub.fix\_encoding

**URL:** <https://discourse.psychopy.org/t/python3-and-iohub-fix-encoding/4056>\
**Category:** Development\
**Created:** [March 4, 2018, 9:34pm UTC](https://discourse.psychopy.org/t/python3-and-iohub-fix-encoding/4056 "2018-03-04T21:34:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tjwilli58](https://avatars.discourse-cdn.com/v4/letter/t/71c47a/32.png) [@tjwilli58](https://discourse.psychopy.org/u/tjwilli58)\
**Post date:** [March 4, 2018, 9:34pm UTC](https://discourse.psychopy.org/t/python3-and-iohub-fix-encoding/4056/1 "2018-03-04T21:34:18Z")

</div>

Copying this from [https://github.com/psychopy/psychopy/issues/1759](https://github.com/psychopy/psychopy/issues/1759)

(with a little edit at the end)

#### 

Trying out 1.90-dev3, using Python 3.6 under Anaconda, I get an error when trying to import iohub:

`TypeError: startswith first arg must be bytes or a tuple of bytes, not str`

I think fix\_win\_sys\_argv(encoding) only needs to be called in Python2 right? How about changing fix\_encoding to add a check for the python version:

```auto
def fix_encoding():
    """Fixes various encoding problems on all platforms.

    Should be called at the very begining of the process.

    """
    ret = True
    if sys.platform == 'win32':
        ret &= fix_win_codec()

    ret &= fix_default_encoding()

    if sys.platform == 'win32' and sys.version_info[0]<3:
        encoding = sys.getdefaultencoding()
        ret &= fix_win_sys_argv(encoding)
        ret &= fix_win_console(encoding)
    return ret

```

#### 

This addresses [Python bugs issue 2128](https://bugs.python.org/issue2128), The last comment there is that the bug fix is not fixable in Python2, the “fix” is to upgrade to Python3.

iohub. fix\_win\_sys\_argv() addresses Issure 2128. I don’t know about iohub.fix\_win\_console, so perhaps the test for Python3 would only be needed for the call to iohub.fix\_win\_sys\_argv().

---

<div class="post-metadata">

**Author:** ![tjwilli58](https://avatars.discourse-cdn.com/v4/letter/t/71c47a/32.png) [@tjwilli58](https://discourse.psychopy.org/u/tjwilli58)\
**Post date:** [March 4, 2018, 10:41pm UTC](https://discourse.psychopy.org/t/python3-and-iohub-fix-encoding/4056/3 "2018-03-04T22:41:47Z")

</div>

Since fix\_win\_sys\_argv() is the function that addresses Python issue 2128, perhaps only that should be version checked.

```
if sys.platform == 'win32':
    encoding = sys.getdefaultencoding()
    if sys.version_info[0] < 3:
        ret &= fix_win_sys_argv(encoding)
    ret &= fix_win_console(encoding)
return ret
```
