WindowsError:[错误2]系统找不到指定的文件Python 2.7

2024-10-05 10:08:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我得到了一组文件,这些文件应该使用Python2脚本和json文件构建一个定制的libswift项目。我不是一名软件工程师,我没有编写这个脚本,我只会python,所以请非常具体地回答。我不认为我必须编辑这个脚本,但因为它构建了我真正需要的东西,我希望编辑这个脚本不会让我陷入困境

我得到了以下指示:

查找MSBuild.exe的位置并将其添加到路径

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

输入命令:python configure.py--config proj\cobra\config\u windows\u vs\u 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%的解决方案并提出了一些建议问题,这里也是 My Path environmental variables

一位同事建议我尝试将打印语句添加到脚本中,因此我对cmake和config数据进行了添加。它看起来像是读取了.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>

这是新的


Tags: nameinpyprojectcmakeconfigjsondata

热门问题