无法从Python脚本调用CMake

时间:2016-10-16 06:50:52

标签: python cmake

我正在尝试从Python脚本调用CMake命令。这是我的代码:

cmakeCmd = ["C:\Program Files\CMake\bin\cmake.exe",'-G Visual Studio 11 Win64', 'C:\Users\MyUser\Desktop\new\myProject']
retCode = subprocess.check_call(cmakeCmd, shell=True)

但是在运行脚本时我得到以下输出:

The filename, directory name, or volume label syntax is incorrect.
Traceback (most recent call last):
  File "C:\Users\ochitaya\Desktop\New\myProj\sc.py", line 10, in <module>
    retCode = subprocess.check_call(cmakeCmd, stderr=subprocess.STDOUT, shell=True)
  File "C:\Python27\lib\subprocess.py", line 541, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['C:\\Program Files\\CMake\x08in\\cmake.exe', '-G Visual Studio 11 Win64', 'C:\\Users\\ochitaya\\Desktop\new\\myProj']' returned non-zero exit status 1

1 个答案:

答案 0 :(得分:1)

Python字符串中的反斜杠(\)是转义字符。这就是字符串"C:\Program Files\CMake\bin\cmake.exe"被翻译为C:\\Program Files\\CMake\x08in\\cmake.exe的原因(注意\b等于\x08)。要解决此问题,请告诉Python您希望按原样读取字符串,或者换句话说,将字符串作为原始字符串,通过在字符串前加r前缀:

cmakeCmd = [r"C:\Program Files\CMake\bin\cmake.exe",'-G Visual Studio 11 Win64', r'C:\Users\MyUser\Desktop\new\myProject']

有关此主题的更多信息,请查看string literals上的官方文档。