如何使用nodegyp构建python?

2024-09-27 18:07:35 发布

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

我已经看过the official tutorialthis这两个版本,它们展示了如何从源代码构建python并使用visual studio等工具对其进行编辑,但是如何使用node gyp构建它以将其用作node中的本机应用程序呢

例如,在本教程中,它们告诉您download the source code,然后(在windows中)在该目录中键入:

"PCbuild/build.bat"   

然后构建VisualStudio解决方案和python等,但是现在如何使用node gyp实现这一点呢?通常,要包含要使用节点gyp构建的外部依赖项,只需将它们包含在binding.gyp文件中。对于Python,我首先安装了64位(后来的32位,下面的错误)python到c:/python(cpythn2),然后我把那个文件夹复制到我的C++应用程序中的另一个文件夹,并将bdId.jip文件设置为这个(以获得包含和LIBs):

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

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位note做了尝试;我不知道如何准确地解决这个问题,或者是否有另一种方法完全用节点gyp构建python

因此:

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


Tags: addonmodulesnodejsloaderusersatdocuments

热门问题