Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (12.7k points)

I'm attempting to utilize pexpect module (version 3.3) with Python 3.4.0. I get an error saying

TypeError: must be str, not bytes

At the point when I call child.expect technique. 

Real code is a standard example from pexpect documentation: 

child = pexpect.spawn('ssh [email protected]')

index = child.expect([pexpect.TIMEOUT, pexpect.EOF, ssh_newkey, '.*password:'])

The very same code works appropriately with pexpect module (version 3.1), and Python version 2.7.6. 

Pexpect documentation on GitHub states that pexpect version 3.3 requires Python 2.6 or 3.2 or above. Can anyone say whether pexpect doesn't work with Python 3 for reasons unknown in spite of what is expressed in the documentation for this module?

Traceback output which I get:

Traceback (most recent call last):

  File "/home/sambo9/python/", line 17, in <module>


  File "/home/sambo9/python/", line 13, in main


  File "/usr/local/lib/python3.4/dist-packages/pexpect/", line 1451, in expect

    timeout, searchwindowsize)

  File "/usr/local/lib/python3.4/dist-packages/pexpect/", line 1466, in expect_list

    timeout, searchwindowsize)

  File "/usr/local/lib/python3.4/dist-packages/pexpect/", line 1535, in expect_loop

    c = self.read_nonblocking(self.maxread, timeout)

  File "/usr/local/lib/python3.4/dist-packages/pexpect/", line 985, in read_nonblocking

    self._log(s, 'read')

  File "/usr/local/lib/python3.4/dist-packages/pexpect/", line 908, in _log


  File "/usr/lib/python3.4/idlelib/", line 1339, in write

    raise TypeError('must be str, not ' + type(s).__name__)

TypeError: must be str, not bytes

Moreover, I additionally see "OpenSSH" box open up through GUI on Ubuntu prompting for a password when I run the content. This doesn't occur under Python 2.7.6. In Python 2.7, I can log in to the framework with no manual communication - everything happens consequently by means of content.

1 Answer

0 votes
by (26.4k points)

From this documentation:

# In Python 3, spawnu should be used to give str to stdout:

child = pexpect.spawnu('some_command')

child.logfile = sys.stdout

Join the python online course fast, to learn python concepts in detail and get certified.

Browse Categories