简体   繁体   English

在我的Python脚本中使用Pexpect时出现错误“ NameError:名称'ExceptionPexpect'未定义”

[英]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.pycpexpect.pycpexpect.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 pexpectapt-get install python-pexpect或其他工具;安装pxssh方式相同)。 If not, you have to do that, too. 如果没有,您也必须这样做。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取“ NameError:名称” <my function> “未定义”错误 - Getting 'NameError: name '<my function>' is not defined' error 执行程序时出现错误“ NameError:名称&#39;TLSExtSupportedGroup&#39;未定义” - Getting Error “NameError: name 'TLSExtSupportedGroup' is not defined” while executing the program 为什么会出现NameError错误:在Python 3中未定义名称&#39;file&#39; - why getting the error of NameError: name 'file' is not defined in python 3 Python 3.5:我收到一个错误:“ NameError:未定义名称&#39;multiprocessing&#39;” - Python 3.5: I am getting an error: “NameError: name 'multiprocessing' is not defined” NameError: name 'my_agg' is not defined 在 python 中使用静态方法时出现错误 - NameError: name 'my_agg' is not defined error when using staticmethod in python NameError: name 'close' is not defined error using Selenium Webdriver through Python - NameError: name 'close' is not defined error using Selenium Webdriver through Python 出现名称错误 NameError,函数未定义? - Getting a name error NameError, function is not defined? NameError: 使用 Selenium 和 Python 未定义名称 'webdriver' 错误 - NameError: name 'webdriver' is not defined error using Selenium and Python 获取 NameError:名称“视频”未在 Vapoursynth/Python 中定义 - Getting NameError: name 'video' is not defined in Vapoursynth/Python 在python中获取未从NameError定义的名称 - Getting the name which is not defined from NameError in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM