TypeError when calling a pexpect module wait method in Python 3

I am trying to use the pexpect module (version 3.3) with Python 3.4.0. I get an error

TypeError: must be str, not bytes

when i call the child.expect method.

Actual code is a standard example from pexpect documentation:

child = pexpect.spawn('ssh foo@bar.com')
index = child.expect([pexpect.TIMEOUT, pexpect.EOF, ssh_newkey, '.*password:'])

Exactly the same code works correctly with the pexpect module (version 3.1) and Python version 2.7.6.

Pexpect's documentation on GitHub claims that pexpect version 3.3 requires Python 2.6 or 3.2 or higher. Does anyone know why pexpect does not work with Python 3 for some reason, despite the fact that the documentation says for this module?

This is the trace output that I get:

Traceback (most recent call last):
  File "/home/sambo9/python/python3-pexpect.py", line 17, in <module>
    main()
  File "/home/sambo9/python/python3-pexpect.py", line 13, in main
    child.expect('.*password:')
  File "/usr/local/lib/python3.4/dist-packages/pexpect/__init__.py", line 1451, in expect
    timeout, searchwindowsize)
  File "/usr/local/lib/python3.4/dist-packages/pexpect/__init__.py", line 1466, in expect_list
    timeout, searchwindowsize)
  File "/usr/local/lib/python3.4/dist-packages/pexpect/__init__.py", line 1535, in expect_loop
    c = self.read_nonblocking(self.maxread, timeout)
  File "/usr/local/lib/python3.4/dist-packages/pexpect/__init__.py", line 985, in read_nonblocking
    self._log(s, 'read')
  File "/usr/local/lib/python3.4/dist-packages/pexpect/__init__.py", line 908, in _log
    second_log.write(s)
  File "/usr/lib/python3.4/idlelib/PyShell.py", line 1339, in write
    raise TypeError('must be str, not ' + type(s).__name__)
TypeError: must be str, not bytes

, , "OpenSSH" GUI Ubuntu, script. Python 2.7.6. Python 2.7 - script.

+4
1

:

# In Python 3, spawnu should be used to give str to stdout:
child = pexpect.spawnu('some_command')
child.logfile = sys.stdout
+3

All Articles