python - 预期方法不起作用

标签 python pexpect

我运行的是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'

预期目录:

>>> import pexpect
>>> dir(pexpect)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']

最佳答案

好吧,对于 Windows 错误,我可以告诉您 Pexpect.spawn 在 Windows 上不起作用。这必须与 pypi 做一些事情,它只在 linux 系统上提供一些模块,而 spawn 使用其中一个部分。

在 Windows 上,您必须改用 PopenSpawn。

关于python - 预期方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39728349/

相关文章:

python - 如何可视化德国的邮政编码数据?

Python 预期 : fdpexpect + pyserial == timeout does not work?

python - 使用 pxssh 时无法为预期导入名称 'spawn'

python 暂停 pexpect.spawn 及其使用的设备

python - 根据一个键查找多键字典的所有键

python - 什么是 Python 的列表[ :x] in C++? 的等价物

python3 : how to use for loop and if statements over class attributes?

python - 需要使用 ElementTree 解析 XML 的帮助

ansible - 它仍然适用于在 RedHat7 上使用 pexpect 还是有替代方法来执行命令并响应提示?

python - pexpect中的问题(python3.3)