【问题标题】:ModuleNotFoundError: No module named 'pyzabbix'ModuleNotFoundError:没有名为“pyzabbix”的模块
【发布时间】:2020-02-10 03:44:16
【问题描述】:

pyzabbix 是此脚本工作所需的模块。我是用 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>

但是,我在第 24 行收到一个错误,即找不到该模块。我不确定我需要做什么。 请参阅下面的脚本并据此提出建议,因为我需要此代码来读取 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()

【问题讨论】:

    标签: python python-3.x python-module zabbix


    【解决方案1】:

    您是否同时安装了 python 2 和 3?如果两者都有,pip 会在python2 下安装模块。如果您希望将模块安装在python3 下,请尝试像这样安装:

    pip3 install pyzabbix
    

    【讨论】:

    • 您好 Emrah,感谢您的评论。我安装了 Python 3.7 和 3.8。在这种情况下,我如何告诉 Pip 在 Python 3.7 上安装 pyzabbix?
    • 我认为您应该在系统上安装pip3.7pip3.8。尝试使用pip3.7 install pyzabbix
    • 我试过了,这就是我得到的 "C:\Users\Christopher Ezimoha>pip3.7 install pyzabbix 要求已经满足:pyzabbix in c:\users\christopher ezimoha\appdata\local\包\pythonsoftwarefoundation.python.3.7_qbz5n2kfra8p0\localcache\local-packages\python37\site-packages (0.7.5)"
    • hmm 好的,接下来要验证的是您正在使用 python3.7 exe 来运行您的 python 代码。
    • 我查过,我用来运行程序的Python解释器是3.7
    【解决方案2】:
    1. 您在问题中包含的代码中没有import
    from pyzabbix import ZabbixAPI
    
    1. login() 函数应该在addHosts() 之外定义,以便像在main() 中那样调用

    【讨论】:

    • 您好 Iron,感谢您的评论,我已添加导入。 "从 pyzabbix 导入 ZabbixAPI,ZabbixAPIException"
    • 你能否解释一下,因为我对 python 很陌生。我已经在 addHost 之外声明了 login() 并且出现了更多错误。
    • 刚刚又试了一次。我仍然遇到同样的错误。 '''Traceback(最近一次调用最后):文件“C:/Users/Christopher Ezimoha/PycharmProjects/untitled/Test.py”,第 21 行,在 from pyzabbix import ZabbixAPI, ZabbixAPIException ModuleNotFoundError: No module named 'pyzabbix ''''
    • 我刚刚尝试过,仍然收到相同的错误“Traceback(最近一次调用最后一次):文件“C:/Users/Christopher Ezimoha/PycharmProjects/untitled/zabbiximport.py”,第 1 行,在 from pyzabbix import ZabbixAPI ModuleNotFoundError: No module named 'pyzabbix'"
    猜你喜欢
    • 2019-03-28
    • 2022-01-07
    • 2017-12-14
    • 2020-12-10
    • 2021-09-02
    • 2020-10-10
    • 2022-01-06
    • 2021-09-10
    相关资源
    最近更新 更多