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

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

示例代码:

try:
    from matplotlib import  pyplot as plt

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

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

相关文章:

  • 2022-02-19
  • 2022-12-23
  • 2021-12-28
  • 2021-09-27
  • 2021-11-30
  • 2022-12-23
  • 2021-12-07
猜你喜欢
  • 2022-12-23
  • 2021-08-27
  • 2021-06-26
  • 2022-12-23
  • 2021-12-09
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案