我很幸运找到了自己问题的答案。在
下面是要使用的正确python调用:src_dir = ... # set your source dir here
build_dir = ... # set your build dir here
# retrieve visual studio installation path using vswhere.
# See: https://github.com/Microsoft/vswhere
vswhere = os.path.join(os.environ['ProgramFiles(x86)'],
"Microsoft Visual Studio",
"Installer",
"vswhere.exe")
# get installation path of Visual Studio 2017
vspath = subprocess.Popen([vswhere, "-property", "installationPath", "-version", "[15,16)"],
stdout=subprocess.PIPE).communicate()[0].rstrip()
# build path to vcvarsall.bat file
vcvarsall = os.path.join(vspath, "VC", "Auxiliary", "Build", "vcvarsall.bat")
# vcvarsall.bat changes the current directory to the one specified
# by the environment variable %VSCMD_START_DIR%
my_env = os.environ
my_env["VSCMD_START_DIR"] = build_dir
# set up the environment and then call cmake with Ninja generator
subprocess.call('call "' + vcvarsall + '" x64 && cmake -G Ninja "' + src_dir + '"', shell=True, env=my_env)