由于用户本地开发的代码需要上传至ModelArts后台,代码中涉及到依赖文件的路径时,用户设置有误的场景较多。
推荐通用的解决方案:使用os接口去获取文件所在的路径,避免报错。
示例:
|---project_root #代码根目录
|---BootfileDirectory #启动文件所在的目录
|---bootfile.py #启动文件
|---otherfileDirectory #其他依赖文件所在的目录
|---otherfile.py #其他依赖文件
在启动文件中,建议用户参考以下方式获取依赖文件所在路径,即示例中的otherfile_path。
import os
current_path = os.path.dirname(os.path.realpath(__file__)) # BootfileDirectory, 启动文件所在的目录
project_root = os.path.dirname(current_path) # 工程的根目录,对应ModelArts训练控制台上设置的代码目录
otherfile_path = os.path.join(project_root, "otherfileDirectory", "otherfile.py")