首页 > 解决方案 > 如何使用 node-gyp 构建 python?

问题描述

我已经看过官方教程以及这个显示如何从源代码构建 python 并使用 Visual Studio 等进行编辑的教程,但是如何使用 node-gyp 构建它以将其用作节点中的本机应用程序?

例如,在教程中,他们告诉您下载源代码,然后(在 Windows 中)在该目录中键入:

"PCbuild/build.bat"   

然后构建了 Visual Studio 解决方案和一般的 python 等,但是现在我如何使用 node-gyp 来做到这一点?通常,要包含使用 node-gyp 构建的外部依赖项,只需将它们包含在 binding.gyp 文件中即可。对于 python,我首先将 64 位(以及后来的 32 位,与以下错误相同)python安装到 C:/Python382,然后将该文件夹复制到我的 C++ 应用程序中的另一个文件夹,并将 binding.gyp 文件设置为此(获取包含和库):

{
  "targets": [
    {
        "target_name": "addon",
        "sources": [ 
            "<!@(node -p \"var fs=require('fs'),path=require('path'),walk=function(r){let t,e=[],n=null;try{t=fs.readdirSync(r)}catch(r){n=r.toString()}if(n)return n;var a=0;return function n(){var i=t[a++];if(!i)return e;let u=path.resolve(r,i);i=r+'/'+i;let c=fs.statSync(u);if(c&&c.isDirectory()){let r=walk(i);return e=e.concat(r),n()}return e.push(i),n()}()};walk('./sources').join(' ');\")"
        ],
        "libraries":[
            "C:/Users/Coby/Documents/aa/atzmus/CPPtesting/other/ok/Python382/libs/python38.lib",
            "C:/Users/Coby/Documents/aa/atzmus/CPPtesting/other/ok/Python382/libs/python3.lib",
            "C:/Users/Coby/Documents/aa/atzmus/CPPtesting/other/ok/Python382/libs/_tkinter.lib"
        ],
        "include_dirs": [
            "C:/Users/Coby/Documents/aa/atzmus/CPPtesting/other/ok/Python382/include"
        ],
        "dll_files": [
            "C:/Users/Coby/Documents/aa/atzmus/CPPtesting/other/ok/Python382/python38.dll"
        ]
    }
  ]
}

我的 hello.cc 文件是另一个来源,它只包含Python.h. 当我使用node-gyp buildor运行这个构建时node-gyp rebuild,它实际上构建得很好,没有任何错误,但是当我将文件 addon.node 复制到我的实际 nodeJS 服务器(只有一行var addon = require("./addon"))时,我在 CMD 中得到以下错误输出(但是,如果不将 Python.h 包含在我的 .cc 源文件中,它可以正常工作且没有错误):

internal/modules/cjs/loader.js:1197
  return process.dlopen(module, path.toNamespacedPath(filename));
                 ^

Error: \\?\C:\Users\Coby\Documents\aa\atzmus\testServer\addon.node is not a valid Win32 application.
\\?\C:\Users\Coby\Documents\aa\atzmus\testServer\addon.node
←[90m    at Object.Module._extensions..node (internal/modules/cjs/loader.js:1197
:18)←[39m
←[90m    at Module.load (internal/modules/cjs/loader.js:983:32)←[39m
←[90m    at Function.Module._load (internal/modules/cjs/loader.js:891:14)←[39m
←[90m    at Module.require (internal/modules/cjs/loader.js:1023:19)←[39m
←[90m    at require (internal/modules/cjs/helpers.js:72:18)←[39m
    at Object.<anonymous> (C:\Users\Coby\Documents\aa\atzmus\testServer\oy.js:2:
9)
←[90m    at Module._compile (internal/modules/cjs/loader.js:1128:30)←[39m
←[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1167:1
0)←[39m
←[90m    at Module.load (internal/modules/cjs/loader.js:983:32)←[39m
←[90m    at Function.Module._load (internal/modules/cjs/loader.js:891:14)←[39m

主要部分似乎是:

addon.node is not a valid Win32 application.

我尝试使用 python 为 64 位 Windows 执行此操作,对于 32 位 Windows,我的系统为 64 位,我的 nodeJS 安装是 64 位,但我也尝试使用 32 位注释;我不知道如何准确解决这个问题,或者是否有另一种完全使用 node-gyp 构建 python 的方法?

所以:

在windows中需要采取哪些步骤来通过node-gyp构建python,以便能够在nodejs中本地使用python而不必求助于子进程?

标签: javascriptpythonnode.jsvisual-studionode-gyp

解决方案


推荐阅读