sys.executable和subprocess.check_all使用案例

本文介绍如何使用sys.executable获取Python解释器路径,并通过subprocess模块的check_call函数执行外部命令。具体展示了如何设置参数,创建训练任务,包括配置参数保存、命令构建和执行过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

sys.executable和subprocess.check_all使用案例。

 sys.executable返回python解释器的路径
PYTHON = sys.executable#'D:\\python3.5.2\\python.exe'

 subprocess模块用来创建新的进程,连接到其stdin、stdout、stderr管道并获取它们的返回码。推荐用户使用callcheck_callcheck_output这三个快捷函数.

运行由args参数提供的命令,等待命令执行结束并返回返回码0,否则抛出。

def launch_training_job(parent_dir, data_dir, job_name, params):
    """Launch training of the model with a set of hyperparameters in parent_dir/job_name

    Args:
        parent_dir: (string) directory containing config, weights and log
        data_dir: (string) directory containing the dataset
        params: (dict) containing hyperparameters
    """
    # Create a new folder in parent_dir with unique_name "job_name"
    model_dir = os.path.join(parent_dir, job_name)
    if not os.path.exists(model_dir):
        os.makedirs(model_dir)

    # Write parameters in json file
    json_path = os.path.join(model_dir, 'params.json')
    params.save(json_path)

    # Launch training with this config
    cmd = "{python} train.py --model_dir {model_dir} --data_dir {data_dir}".format(python=PYTHON,
            model_dir=model_dir, data_dir=data_dir)
    print(cmd)
    check_call(cmd, shell=True)

参考https://blog.youkuaiyun.com/a464057216/article/details/47355219

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值