我正在运行CentOS7并在Python3.5中安装了pexpect。但是,当我调用任何方法时,它返回一个错误,指出该属性不存在。有没有想过为什么会这样?我读到它可能是由于目录中的文件名pexpect.py,但我没有在同一目录中有一个名为pexpect.py的文件。
$ pip3.5 freeze | grep pexpect
pexpect==4.2.1
示例代码:
# This connects to the openbsd ftp site and
# downloads the recursive directory listing.
import pexpect
child = pexpect.spawn('ftp ftp.openbsd.org')
child.expect('Name .*: ')
child.sendline('anonymous')
child.expect('Password:')
child.sendline('noah@example.com')
child.expect('ftp> ')
child.sendline('lcd /tmp')
child.expect('ftp> ')
child.sendline('cd pub/OpenBSD')
child.expect('ftp> ')
child.sendline('get README')
child.expect('ftp> ')
child.sendline('bye')
错误:
CentOS7虚拟机错误:
/usr/local/bin/python3.5 /media/sf_PycharmProjects/MyPyScripts/Tutorials/input_cmds
Traceback (most recent call last):
File "/media/sf_PycharmProjects/MyPyScripts/Tutorials/input_cmds", line 4, in <module>
child = pexpect.spawn('ftp ftp.openbsd.org')
AttributeError: module 'pexpect' has no attribute 'spawn'
Windows错误:
Traceback (most recent call last):
File "C:/Users/home/PycharmProjects/PyCAT/Current_Version/SFTP/testsftp.py", line 4, in <module>
child = pexpect.spawn('ftp ftp.openbsd.org')
AttributeError: module 'pexpect' has no attribute 'spawn'
Pexpect Dir:
>>> import pexpect
>>> dir(pexpect)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']
答案 0 :(得分:1)
好吧,对于Windows错误,我可以告诉你,Pexpect.spawn在Windows上不起作用。这必须与pypi做一些事情,它只在linux系统上提供一些模块,spawn
使用其中一个部分。
在Windows上,您必须使用PopenSpawn。