[英]Getting error “NameError: name 'ExceptionPexpect' is not defined” while using Pexpect in my Python scripts
I tried to run following piece of code on my server, 我试图在服务器上运行以下代码,
import pexpect
import pxssh
import getpass
s = pxssh.pxssh()
hostname = raw_input("hostname:" )
username = raw_input("Username:" )
password = getpass.getpass("password:" )
if not s.login('hostname', 'Username', 'password'):
print ("SSH login failed")
print str(s)
else:
print ("You logged in successfully")
s.sendline('uptime')
s.prompt()
print s.before
s.exit()
But, Whenever I try to execute the code, I see following error message, 但是,每当我尝试执行代码时,都会看到以下错误消息,
Traceback (most recent call last):
File "pexpect.py", line 1, in <module>
import pexpect
File "/export/rahul_lab/Python/pexpect.py", line 2, in <module>
import pxssh
File "/usr/lib/python2.6/site-packages/pxssh.py", line 14, in <module>
class ExceptionPxssh(ExceptionPexpect):
NameError: name 'ExceptionPexpect' is not defined
Can somebody guide, what is the issue here and why am I getting this error all the time? 有人可以指导吗,这是什么问题?为什么我总是一直收到此错误?
The problem is that you have a pexpect.py
file in your working directory. 问题是您的工作目录中有一个
pexpect.py
文件。 (Maybe even this very script?) (也许甚至是这个脚本?)
That hides the pexpect
module that you've installed to your site-packages. 这将隐藏已安装到站点包中的
pexpect
模块。 Which pxssh
needs. 需要哪个
pxssh
。
The solution is to rename your pexpect.py
file to something different. 解决方案是将您的
pexpect.py
文件重命名为其他名称。 And check if there's a pexpect.pyc
or pexpect.pyo
left behind and, if so, delete those. 并检查是否
pexpect.pyc
了pexpect.pyc
或pexpect.pyo
,如果存在,请删除它们。
Of course I'm assuming you actually installed pexpect
to your site-packages in the first place (with sudo pip install pexpect
or apt-get install python-pexpect
or whatever; the same way you installed pxssh
). 当然,我假设您实际上
pexpect
在站点包中sudo pip install pexpect
(使用sudo pip install pexpect
或apt-get install python-pexpect
或其他工具;安装pxssh
方式相同)。 If not, you have to do that, too. 如果没有,您也必须这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.