简体   繁体   English

如何修复“没有名为“ easysnmp”的模块”

[英]How do I fix “No module named 'easysnmp'”

when I try to run the code. 当我尝试运行代码时。 I am getting an error. 我收到一个错误。

The error is 错误是

  Traceback (most recent call last): File "A2.py", line 2, in <module> from easysnmp import Session ImportError: No module named 'easysnmp' 

Note: I am getting the above error.Even though, I have installed easysnmp module. 注意:我遇到了以上错误,即使我已经安装了easysnmp模块。

The code is 该代码是

 #!/usr/bin/python
from easysnmp import Session
import argparse
import time
parser = argparse.ArgumentParser(description='probe')
parser.add_argument('cred',help='credentials')
parser.add_argument('freq',type=float,help='enter frequency')
parser.add_argument('samples',type=int,help='enter samples')
parser.add_argument('oid',nargs='+',help='enter oid')
args=parser.parse_args()
t=1/args.freq
s=args.samples
cred1=args.cred
ip,port,comm=cred1.split(":")
count=0
session=Session(hostname=ip,remote_port=port,community=comm, version=2,timeout=2,retries=1)
args.oid.insert(0, '1.3.6.1.2.1.1.3.0')
old=[]
out1=[]
t4=0
while (count!=s):
  t1=time.time()
  new = session.get(args.oid)
  t2=time.time()
  if len(new)==len(old):
   newtime=float(new[0].value)/100
   oldtime=float(old[0].value)/100
   if args.freq > 1:
     tdiff = newtime-oldtime
   if args.freq <= 1:
     tdiff1 = t1-t4
     if tdiff!=0:
      tdiff = int(tdiff1)
     else:
      tdiff = int(t)
   for i in range(1,len(args.oid)):
      if new[i].value!="NOSUCHINSTANCE" and old[i].value!="NOSUCHINSTANCE":
         a=int(new[i].value)
         b=int(old[i].value)
         if a>=b:
           out=(a-b)/tdiff
           out1.append(out)
         if a<b and new[i].snmp_type=="COUNTER64":
           out=((2**64+a)-b)/tdiff
           out1.append(out)
         if a<b and new[i].snmp_type=="COUNTER32":
           out=((2**32+a)-b)/tdiff
           out1.append(out)
      else:
        print t1, "|"
   count=count+1
   if len(out1)!=0:
      sar = [str(get) for get in out1]
      print int(t1) ,'|', ("|" . join(sar))
  old = new[:]
  t4=t1
  del out1[:]
  t3=time.time()
  if t-t3+t1>0:
    time.sleep(t-t3+t1)
  else:
    time.sleep(0.0)

尝试将import easysnmp放在代码的顶部,在类似情况下它为我解决了问题!

My wild guess is that you installed the module for python 3 and used python 2 or the other way around. 我的疯狂猜测是您为python 3安装了模块,并使用了python 2或其他方法。

Try 尝试

pip install easysnmp

or 要么

pip3 install easysnmp

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM