X
    Categories: Python

判断 Python 模块是否安装

在不加载 module 的情况下判断 module 是否已安装

import imp

class checkModule:
    def check_psutil(self):
        try:
            imp.find_module('psutil')
            found = True
        except ImportError:
            found = False

        if not found:
            raise AssertionError('Module psutil is not installed.')
        else:
            print('Module psutil is installed.')

related post

Edwin: