pyserial ser.write('string') TypeError in OS X

Diez B. Roggisch deets at
Tue Oct 20 23:38:02 CEST 2009

Rodrigo schrieb:
> Maybe this is not a bug at all, but i have installed python2.5. 3.01
> and 3.1.1. In python 2.5 ser. write('this is a string') works just
> fine.
> On the other hand, with 3.01 and 3.1.1 (pyserial 2.5 rc1) when i do a
> ser.write('this is a string') i get the following error"
>>>> import serial
>>>> ser = serial.Serial('/dev/tty.usbserial')
>>>> ser.write('this is a string')
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "/Library/Frameworks/Python.framework/Versions/3.1/lib/
> python3.1/site-packages/serial/", line 466, in write
>     raise TypeError('expected %s or bytearray, got %s' % (bytes, type
> (data)))
> TypeError: expected <class 'bytes'> or bytearray, got <class 'str'>
> I honestly dont get what im doing wrong here

In Python3, the standard type for string-literals is not a byte-string, 
but unicode. Thus you need to explicitly encode the passed value to a 
bytes-object, as that's what is allowd for communicating with the 
uoutside world.

   ser.write('this is a string'.encode('utf-8'))

should do the trick.

The error-message is an abomination though...


More information about the Python-list mailing list