Rap*_*l10 3 python child-process node.js webpack copy-webpack-plugin
在一个 electron.js 应用程序中,我试图根据这些指示从 node.js 调用和执行 python 脚本:https : //nodejs.org/api/child_process.html#child_process_child_process_execfile_file_args_options_callback
import { execFile } from 'child_process';
let pythonPath = '/home/marco/anaconda3/bin/python3';
execFile('./scripts/factorial.py',
[parseInt(msg)],
{ cwd: './scripts',
env: '/home/marco/anaconda3/bin/python3'
},
(error, stdout, stderr) => {
if (error) {
throw error;
}
console.log(stdout);
}
);
Run Code Online (Sandbox Code Playgroud)
我得到这个错误:
评论options部分时:
execFile('./scripts/factorial.py',
[parseInt(msg)],
//{ cwd: './scripts',
//env: '/home/marco/anaconda3/bin/python3'
//},
(error, stdout, stderr) => {
if (error) {
throw error;
}
console.log(stdout);
}
);
Run Code Online (Sandbox Code Playgroud)
我犯了同样的错误。
按照此处找到的指示:https : //www.tutorialspoint.com/run-python-script-from-node-js-using-child-process-spawn-method 我修改了代码如下:
const { spawn } = require('child_process');
let nb = parseInt(msg);
function runScript() {
return spawn ('python3', [
"-u",
path.join(__dirname, './scripts/factorial.py'),
nb,
]);
}
const subprocess = runScript();
subprocess.stdout.on('data', (data) => {
console.log(`data:${data}`);
});
subprocess.stderr.on('data', (data) => {
console.log(`error:${data}`);
});
subprocess.stderr.on('close', () => {
console.log("Closed");
});
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
error:python3: can't open file '/home/marco/webMatters/electronMatters
/PythonConnection/.webpack/main/scripts/factorial.py': [Errno 2] No
such file or directory
Closed
Run Code Online (Sandbox Code Playgroud)
按照此处找到的指示:https ://webpack.js.org/plugins/copy-webpack-plugin/#root 我将 /tools/webpack/webpack.plugins.js 修改为:
const CopyPlugin = require("copy-webpack-plugin");
module.exports = [
new ForkTsCheckerWebpackPlugin(),
new webpack.ExternalsPlugin('commonjs', [
'electron'
]),
new CopyPlugin({
patterns: [
{ from: "src/scripts/", to: ".webpack/main/script/" },
],
}),
];
Run Code Online (Sandbox Code Playgroud)
对我来说,它似乎尊重标准,但我收到了这么长的错误消息:
An unhandled error has occurred inside Forge:
compilation.getCache is not a function
TypeError: compilation.getCache is not a function
at /home/marco/webMatters/electronMatters/PythonConnection
/node_modules/copy-webpack-plugin/dist/index.js:459:33
at SyncHook.eval (eval at create (/home/marco/webMatters
/electronMatters/PythonConnection/node_modules/tapable
/lib/HookCodeFactory.js:19:10), <anonymous>:7:1)
at SyncHook.lazyCompileHook (/home/marco/webMatters/electronMatters
/PythonConnection/node_modules/tapable/lib/Hook.js:154:20)
at Compiler.newCompilation (/home/marco/webMatters/electronMatters
/PythonConnection/node_modules/webpack/lib/Compiler.js:630:30)
at /home/marco/webMatters/electronMatters/PythonConnection
/node_modules/webpack/lib/Compiler.js:667:29
at AsyncSeriesHook.eval [as callAsync] (eval at create (/home/marco
/webMatters/electronMatters/PythonConnection/node_modules/tapable
/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
at AsyncSeriesHook.lazyCompileHook (/home/marco/webMatters
/electronMatters/PythonConnection/node_modules/tapable
/lib/Hook.js:154:20)
at Compiler.compile (/home/marco/webMatters/electronMatters
/PythonConnection/node_modules/webpack/lib/Compiler.js:662:28)
at /home/marco/webMatters/electronMatters/PythonConnection
/node_modules/webpack/lib/Watching.js:77:18
at AsyncSeriesHook.eval [as callAsync] (eval at create (/home/marco
/webMatters/electronMatters/PythonConnection/node_modules/tapable
/lib/HookCodeFactory.js:33:10), <anonymous>:33:1)
at AsyncSeriesHook.lazyCompileHook (/home/marco/webMatters
/electronMatters/PythonConnection/node_modules/tapable
/lib/Hook.js:154:20)
at Watching._go (/home/marco/webMatters/electronMatters
/PythonConnection/node_modules/webpack/lib/Watching.js:41:32)
at /home/marco/webMatters/electronMatters/PythonConnection
/node_modules/webpack/lib/Watching.js:33:9
at Compiler.readRecords (/home/marco/webMatters/electronMatters
/PythonConnection/node_modules/webpack/lib/Compiler.js:529:11)
at new Watching (/home/marco/webMatters/electronMatters
/PythonConnection/node_modules/webpack/lib/Watching.js:30:17)
at Compiler.watch (/home/marco/webMatters/electronMatters
/PythonConnection/node_modules/webpack/lib/Compiler.js:244:10)
error Command failed with exit code 1.
Run Code Online (Sandbox Code Playgroud)
似乎更进一步,但仍有一些问题需要解决
节点版本:v14.5.0
操作系统:Ubuntu 18.04 Desktop
如何解决问题?期待您的帮助
小智 9
似乎您的问题是您使用的 webpack < 5 和 copy-webpack-plugin 7 不兼容。
做就是了 :
npm install copy-webpack-plugin@6 --save-dev
Run Code Online (Sandbox Code Playgroud)
要解决这个问题。
| 归档时间: |
|
| 查看次数: |
2202 次 |
| 最近记录: |