My pexpect script is quite simple, but I hit "TypeError: write() argument must be str, not bytes" after child.interact()
child = pexpect.spawn('npm login', timeout=40, encoding='utf-8')
child.logfile_read = sys.stdout
child.expect('Username:')
child.sendline('qiulang2000')
child.expect('Password:')
child.sendline('xxxx')
...
child.interact()
The error message:
File "/Users/qiulang/PycharmProjects/pexpect-test/./main.py", line 63, in <module>
main()
File "/Users/qiulang/PycharmProjects/pexpect-test/./main.py", line 59, in main
npm_login()
File "/Users/qiulang/PycharmProjects/pexpect-test/./main.py", line 54, in npm_login
child.interact()
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pexpect/pty_spawn.py", line 793, in interact
self.__interact_copy(escape_character, input_filter, output_filter)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pexpect/pty_spawn.py", line 838, in __interact_copy
self._log(data, 'read')
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pexpect/spawnbase.py", line 133, in _log
second_log.write(s)
TypeError: write() argument must be str, not bytes
I know what that error meant, e.g. as TypeError: write() argument must be str, not bytes (Python 3 vs Python 2 ) explained, but how do I fix that for child.interact() ?
I change my codes to
child = pexpect.spawn('npm login', timeout=40)
child.logfile_read = sys.stdout.buffer
...
BUT then pexpect echo twice for every message after interact()! How do I fix that then ?