Python和Pexpect pxsh已安装,但不可用

2024-05-16 02:17:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我和这张海报有同样的问题:Can't import pxssh from pexpect

然而,对上述海报有效的解决方案对我不起作用。在

问题是。。。我是一个Python新手,正在编写Python脚本(python2.7.5),并安装了pexpect4.6.0。我可以在代码中导入和使用pexpect。但是,我需要使用pxsh,但这不起作用。下面是我的代码:

Linux $ ./myCode.py
Traceback (most recent call last):
  File "./myCode.py", line 4, in <module>
    from pexpect import pxssh
ImportError: cannot import name pxssh
Linux $

代码如下:

^{pr2}$

所以我相信我正确地包含了pxsh库,并且在代码中正确地调用了类。问题是,虽然我的系统上安装了pexpect,但pxssh不可用。我不明白为什么。在

当我搜索我的系统时,我看到一个“pxsh.py文件“pexpect目录中的文件:

Linux $ pwd
/home/me/project1/lib/python3.6/site-packages/pexpect-4.6.0-py3.6.egg/pexpect
Linux $
Linux $ ls -l
total 200
-rw-r--r--. 1 ph9821 domain users 12177 Sep 21 15:58 ANSI.py
-rw-r--r--. 1 ph9821 domain users  2685 Sep 21 15:58 _async.py
-rw-rw-r--. 1 ph9821 domain users   380 Sep 21 15:58 bashrc.sh
-rw-r--r--. 1 ph9821 domain users  1068 Sep 21 15:58 exceptions.py
-rw-r--r--. 1 ph9821 domain users 11035 Sep 21 15:58 expect.py
-rw-r--r--. 1 ph9821 domain users  5828 Sep 21 15:58 fdpexpect.py
-rw-r--r--. 1 ph9821 domain users 13419 Sep 21 15:58 FSM.py
-rw-r--r--. 1 ph9821 domain users  3902 Sep 21 15:58 __init__.py
-rw-r--r--. 1 ph9821 domain users  6161 Sep 21 15:58 popen_spawn.py
-rw-r--r--. 1 ph9821 domain users 35855 Sep 21 15:58 pty_spawn.py
-rw-r--r--. 1 ph9821 domain users 22589 Sep 21 15:58 pxssh.py        <<<====
drwxr-xr-x. 2 ph9821 domain users  4096 Sep 21 15:58 __pycache__
-rw-r--r--. 1 ph9821 domain users  5170 Sep 21 15:58 replwrap.py
-rw-r--r--. 1 ph9821 domain users  6632 Sep 21 15:58 run.py
-rw-r--r--. 1 ph9821 domain users 13716 Sep 21 15:58 screen.py
-rw-r--r--. 1 ph9821 domain users 21067 Sep 21 15:58 spawnbase.py
-rw-r--r--. 1 ph9821 domain users  6019 Sep 21 15:58 utils.py

Linux $

在另一篇文章中,他们建议检查一下我是否需要升级,但这没有帮助:

Linux $ easy_install --upgrade pexpect
Searching for pexpect
Reading https://pypi.python.org/simple/pexpect/
Best match: pexpect 4.6.0
Processing pexpect-4.6.0-py3.6.egg
pexpect 4.6.0 is already the active version in easy-install.pth

Using /home/me/project1/lib/python3.6/site-packages/pexpect-4.6.0-py3.6.egg
Processing dependencies for pexpect
Finished processing dependencies for pexpect
Linux $

所以。。。我使用的是Python的兼容版本,安装并运行了最新的pexpect,并且pxsh.py文件在正确的地方。但是,代码不知道如何使用pxsh。在

我还应该说pxsh从未在这台机器上运行过。在安装了pexpect之后,我需要做些什么来启动pxssh吗?在

这可能是python2.7.5和pexpect4.6.0之间的版本兼容性问题吗?我注意到在我的pexpect安装路径中有一个对python3.6的引用。。。在

附录:

我还注意到,当我将代码改为:

#!/usr/bin/python

import sys, time, datetime, logging, pexpect
from pexpect import *
...
s = pexpect.pxssh()
s = pexpect.pxssh(options={"StrictHostKeyChecking": "no", "UserKnownHostsFile": "/dev/null"})
...

错误消息更改为:

Linux $ ./myCode
Traceback (most recent call last):
  File "./myCode.py", line 188, in <module>
    s = pexpect.pxssh()
AttributeError: 'module' object has no attribute 'pxssh'
Linux $

所以代码可以使用pexpect,但不能使用pxsh。也就是说没有安装pxssh???真的很困惑。。。在


Tags: 代码infrompyimportdomainlinuxusers