首页 > 解决方案 > WindowsError: [错误 2] 系统找不到指定的文件,Python 2.7

问题描述

我得到了一组文件,这些文件应该使用 python 2 脚本和 json 文件构建自定义 libswift 项目。我不是软件工程师,我没有制作这个脚本,我只擅长 python,所以请非常具体地回答任何问题。我不认为我应该编辑脚本,但因为它构建了我真正需要的东西,所以我希望编辑这个脚本不会让我失望。

我得到了这个指示:

找到 MSBuild.exe 的位置并将其添加到 PATH

在 libswift 目录中打开命令提示符

输入命令:python configure.py --config proj\cobra\config_windows_vs_2017.json

这应该创建一个文件位置和 .sln 文件,然后与 Visual Studio 一起使用这里是 python 脚本:

#!/usr/bin/env python

import argparse
import json
import os

from subprocess import call

parser = argparse.ArgumentParser(description="Configure a libswift build")

parser.add_argument("--config", type=file, required=True)

class Configure_Args(object):
    pass

args = Configure_Args()

parser.parse_args(namespace=args);

config_data = json.load(args.config)

if(not "project_name" in config_data):
    raise SystemExit("A project_name definition does not exist in the loaded config file!")

project_name = config_data["project_name"]

if(not "platform_name" in config_data):
    raise SystemExit("A platform_name definition does not exist in the loaded config file!")

platform_name = config_data["platform_name"]



cmake_args = ["cmake"]

if("cmake_generator" in config_data):
    cmake_args.extend(["-G", config_data["cmake_generator"]])

cmake_args.append("-DLIBSWIFT_PROJECT_NAME:STRING=" + project_name)
cmake_args.append("-DLIBSWIFT_PROJECT_PLATFORM:STRING=" + platform_name)

if("cmake_c_compiler" in config_data):
    cmake_args.append("-DCMAKE_C_COMPILER:STRING=" + config_data["cmake_c_compiler"])

if("cmake_cxx_compiler" in config_data):
    cmake_args.append("-DCMAKE_CXX_COMPILER:STRING=" + config_data["cmake_cxx_compiler"])

if("cmake_system_name" in config_data):
    cmake_args.append("-DCMAKE_SYSTEM_NAME:STRING=" + config_data["cmake_system_name"])

if("modules" in config_data):
    module_sources = ""
    module_headers = ""
    for module in config_data["modules"]:
        module_sources += module + ".cpp;"
        module_headers += "headers/" + module + ".h;"
        if os.path.exists("./modules/" + module + "/config.json"):
            module_data = json.load(open("./modules/" + module + "/config.json"))
            if "module" in module_data and module_data["module"] == module:
                if "sources" in module_data:
                    for src in module_data["sources"]:
                        module_sources += src + ";"
                if "headers" in module_data:
                    for src in module_data["headers"]:
                        module_headers += "headers/" + src + ";"
    print("Modules: {}".format(module_sources))
    cmake_args.append("-DLIBSWIFT_MODULE_SOURCES:LIST={}".format(module_sources))
    cmake_args.append("-DLIBSWIFT_MODULE_HEADERS:LIST={}".format(module_headers))
    
if("config_definitions" in config_data):
    for key,value in config_data["config_definitions"].iteritems():
        cmake_args.append("-D{}={}".format(key, value))

cmake_args.append("-DLIBSWIFT_RAN_FROM_PYTHON_CONFIGURE_SCRIPT=1")

if not os.path.exists("./gen"):
    os.makedirs("./gen")

if not os.path.exists("./gen/" + project_name):
    os.makedirs("./gen/" + project_name)

if not os.path.exists("./gen/" + project_name + "/" + platform_name):
    os.makedirs("./gen/" + project_name + "/" + platform_name)

if("cmake_generator" in config_data and config_data["cmake_generator"] == "Unix Makefiles"):
    makefile = open("./Makefile", 'w')
    makefile.write("all:\n\tcd ./gen/" + project_name + "/" + platform_name + " && $(MAKE) --no-print-directory $(ARGS)")

if("cmake_generator" in config_data and config_data["cmake_generator"] == "NMake Makefiles"):
    makefile = open("./Makefile", 'w')
    makefile.write("all:\n\tcd .\\gen\\" + project_name + "\\" + platform_name + " && nmake $(ARGS)")

os.chdir("./gen/" + project_name + "/" + platform_name)

cmake_args.append("../../..")

call(cmake_args)

我像这样在命令提示符下调用它

Microsoft Windows [Version 10.0.19042.985]
(c) Microsoft Corporation. All rights reserved.

C:\libswift>python configure.py --config proj\cobra\config_windows_vs_2017.json
Modules: system.cpp;firmware.cpp;cobra.cpp;mec.cpp;
Traceback (most recent call last):
  File "configure.py", line 97, in <module>
    call(cmake_args)
  File "C:\Program Files\Python27\lib\subprocess.py", line 486, in call
    return Popen(*popenargs, **kwargs).wait()
  File "C:\Program Files\Python27\lib\subprocess.py", line 672, in __init__
    errread, errwrite)
  File "C:\Program Files\Python27\lib\subprocess.py", line 882, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

我已经查找了解决方案,并且对 %PATH% 提出了一些建议问题,所以这也是 我的路径环境变量

一位同事建议我尝试在脚本中添加打印语句,所以我为 cmake 和配置数据做了。看起来它读取了 .json 文件,所以我不确定它找不到哪个文件。是 subprocess.py 寻找的东西吗?

我还尝试使用 configure.py 文件将 .json 文件移动到目录的顶部。

这是新的命令提示符输出

Microsoft Windows [Version 10.0.19042.985]
(c) Microsoft Corporation. All rights reserved.

C:\libswift>python configure.py --config config_windows_vs_2017.json
Modules: system.cpp;firmware.cpp;cobra.cpp;mec.cpp;

cmake:  ['cmake', '-G', 'Visual Studio 15 2017', '-DLIBSWIFT_PROJECT_NAME:STRING=cobra', '-DLIBSWIFT_PROJECT_PLATFORM:STRING=msvc_vs', '-DLIBSWIFT_MODULE_SOURCES:LIST=system.cpp;firmware.cpp;cobra.cpp;mec.cpp;', '-DLIBSWIFT_MODULE_HEADERS:LIST=headers/system.h;headers/firmware.h;headers/cobra.h;headers/mec.h;', '-DLIBSWIFT_BUILD_STRS=1', '-DLIBSWIFT_BUILD_HOST=1', '-DLIBSWIFT_BUILD_TESTS=1', '-DLIBSWIFT_BUILD_TOOLS=1', '-DLIBSWIFT_RAN_FROM_PYTHON_CONFIGURE_SCRIPT=1', '../../..']

config_data:  {'platform_name': 'msvc_vs', 'modules': ['system', 'firmware', 'cobra'], 'project_name': 'cobra', 'cmake_generator': 'Visual Studio 15 2017', 'config_definitions': {'LIBSWIFT_BUILD_STRS': 1, 'LIBSWIFT_BUILD_HOST': 1, 'LIBSWIFT_BUILD_TESTS': 1, 'LIBSWIFT_BUILD_TOOLS': 1}}

Traceback (most recent call last):
  File "configure.py", line 102, in <module>
    call(cmake_args)
  File "C:\Program Files\Python27\lib\subprocess.py", line 486, in call
    return Popen(*popenargs, **kwargs).wait()
  File "C:\Program Files\Python27\lib\subprocess.py", line 672, in __init__
    errread, errwrite)
  File "C:\Program Files\Python27\lib\subprocess.py", line 882, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

C:\libswift>

这是新的

标签: pythonjsonwindowspython-2.7command-prompt

解决方案


推荐阅读