sys.executable和subprocess.check_all使用案例。
sys.executable返回python解释器的路径
PYTHON = sys.executable#'D:\\python3.5.2\\python.exe'
subprocess模块用来创建新的进程,连接到其stdin、stdout、stderr管道并获取它们的返回码。推荐用户使用call
、check_call
和check_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)