Python and Pexpect -- Pxssh is installed but not available

1.7k views Asked by At

I have the same problem as this poster: Can't import pxssh from pexpect

However, the solutions which worked for the above poster are not working for me.

Here's the problem... I am a Python newbie writing a Python script (Python 2.7.5) and have installed pexpect 4.6.0. I can import and use pexpect in my code. However, I need to use pxssh, and that is NOT working. Here's my code blowing up:

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 $

The code is this:

#!/usr/bin/python

import sys, time, datetime, logging
from pexpect import pxssh

...
s = pxssh.pxssh()
s = pxssh.pxssh(options={"StrictHostKeyChecking": "no", "UserKnownHostsFile": "/dev/null"})
...more stuff...

So I believe I'm including the pxssh library correctly and I'm invoking the class correctly in my code. The problem is that although pexpect is installed on my system, pxssh isn't available. I can't figure out why.

When I search my system, I see a "pxssh.py" file in the pexpect directory:

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 $

In the other post, they recommended checking to see if I needed an upgrade, but that didn't help:

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 $

So... I'm on a compatible version of Python, with the latest pexpect installed and working, and pxssh.py is in the right place. And yet, the code doesn't know how to use pxssh.

I should also say that pxssh has never worked on this machine. Is there something I have to do to kick pxssh after pexpect is installed?

Could this be some kind of version compatibility issue between Python 2.7.5 and pexpect 4.6.0? I note that there is a reference to Python 3.6 in my pexpect installation path...

ADDENDUM:

I also note that when I change the code to this:

#!/usr/bin/python

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

That the error message changes to this:

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 $

So the code can use pexpect but not pxssh. Which means pxssh isn't installed??? Really confused...

1

There are 1 answers

0
Veeksha A V On
import pxssh

s = pxssh.pxssh()

The above code snippet should work,try importing only pxssh module.