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.')

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.