一般做自动化测试我们直接执行最后一个脚本,通过它调用各个自动化测试脚本,我们的自动化测试脚本可能是分布在各个文件夹中的,不同的模块我们通过from XX import YY引入。如果在编辑软件中我们可以直接执行即可,但是如果想通过批处理文件启动脚本或是通过cmd启动脚本,此时就会提示找不到模块,也就是import失败。处理办法如下:
import sys sys.path.append("D:\workspace\thinksns\src") #在项目运行时,临时将项目路径添加到环境变量,这种方法在脚本运行完成后就会失效 ,不加这句无法用cmd直接运行该脚本,会提示找不到模块也就是import失败
就如上述所示在批处理文件或是cmd即将启动的.py文件中的最上面加入上面2句话,路径就是你项目代码所在的路径,添加以后再用批处里文件或是cmd启动时就不会报错啦