Python如果导出失败,pass函数功能

由于服务器中有一些模块不存在,在文件中导入这些模块时提示错误,导致本地运行正常,服务器测试不通过。

此时,需要捕捉ImportError,当导入的包不存在时,pass掉定义的功能。

示例代码:

try:
    from matplotlib import  pyplot as plt

    def add():
        print('import success')
except ImportError:
    def add():
        pass

这样,当matplotlib模块不存在时,也不会导致报错。

posted @ 2017-08-24 08:20  你听的到  阅读(342)  评论(0编辑  收藏  举报