[英]ModuleNotFoundError: No module named 'pyzabbix'
pyzabbix
is a module needed for this script to work. pyzabbix
是此脚本工作所需的模块。 I installed it using pip, please see a confirmation below:我使用 pip 安装它,请参阅下面的确认:
WARNING: The script chardetect.exe is installed in 'C:\Users\Christopher Ezimoha\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\Scripts' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed certifi-2019.11.28 chardet-3.0.4 idna-2.8 pyzabbix-0.7.5 requests-2.22.0 urllib3-1.25.8
C:\Users\Christopher Ezimoha\Desktop>
However, I'm getting an error at line 24 that the module can't be found.但是,我在第 24 行收到一个错误,提示找不到该模块。 I'm not sure what I need to do.
我不确定我需要做什么。 Please see the script below and advise accordingly as I need this code to read a CSV file and update an application called
Zabbix
.请参阅下面的脚本并提出相应建议,因为我需要此代码来读取 CSV 文件并更新名为
Zabbix
的应用程序。
def addHosts(zapi):
# Add Hosts
file = open('hosts.csv', 'r')
reader = csv.reader(file)
devicelist = list(reader)
import csv
def login():
# Login/authenticate below
session = ZabbixAPI('https://zabbix.xxxxxx.xxx')
# session = ZabbixAPI('http://xxxxxxxxxx/zabbix')
session.login(user="xxxxxxxxxxxx", password="xxxxxxxx")
print("Connected to Zabbix API Version %s" % session.api_version())
return session
for row in devicelist:
device = row[0]
hostgroup = row[1]
responsegroup = zapi.hostgroup.get(filter={'name': hostgroup})
groupid = responsegroup[0]['groupid']
ip = row[2]
templatePriority = row[3]
responsepriority = zapi.template.get(filter={'name': templatePriority})
templatePriorityId = responsepriority[0]['templateid']
# if templatePriority == 'P1':
# templatePriorityId = '40874'
templateType = row[4]
responsetype = zapi.template.get(filter={'name': templateType})
templateTypeId = responsetype[0]['templateid']
try:
response = zapi.host.create(
host=device,
interfaces=[{
'type': 2,
'main': 1,
'ip': ip,
'dns': '',
'port': 161,
'useip': 1
}],
groups=[{
'groupid': groupid}],
templates=[{'templateid': templatePriorityId}, {'templateid': templateTypeId}],
inventory_mode=1
)
print("Created new host: " + device)
except ZabbixAPIException as e:
if 'already exists' in e[0]:
print("Already created host " + device)
else:
print(e[0])
return
def main():
# hostgroup = raw_input('Hostgroup: ')
#hostgroup = "ALTC - Altcar"
zapi = login()
addHosts(zapi)
return
if __name__ == '__main__':
main()
Do you have both python 2 and 3 installed?您是否同时安装了 python 2 和 3? If you have both,
pip
installs modules under python2
.如果两者都有,
pip
会在python2
下安装模块。 Try installing like this if you'd like the module to be installed under python3
:如果您希望将模块安装在
python3
下,请尝试像这样安装:
pip3 install pyzabbix
import
, in the code you included in the question:import
:from pyzabbix import ZabbixAPI
login()
function should be defined outside of addHosts()
, in order to be called like that in main()
login()
函数应该在addHosts()
之外定义,以便像在main()
那样调用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.