繁体   English   中英

从 electron 应用程序运行 python 脚本时如何修复“错误:生成 py ENOENT”错误?

[英]How to fix "Error: spawn py ENOENT" error when running python script from electron app?

我试图从 windows 10 中的 electron 应用程序运行一些 python 脚本。我试图运行的示例代码是:

let {PythonShell} = require('python-shell')
PythonShell.run('test.py',  function  (err, results)  {
 if  (err)  throw err;
 console.log('test.py finished.');
 console.log('results', results);
});

该代码预计运行 test.py ,其中包含一个简单的打印语句。但终端显示以下错误日志:

throw er; // Unhandled 'error' event
      ^

Error: spawn py ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:240:19)
    at onErrorNT (internal/child_process.js:415:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)
    at Function.Module.runMain (internal/modules/cjs/loader.js:745:11)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
Emitted 'error' event at:
    at Process.ChildProcess._handle.onexit (internal/child_process.js:246:12)
    at onErrorNT (internal/child_process.js:415:16)
    [... lines matching original stack trace ...]
    at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)

看起来 Python 可执行文件在您的节点脚本中不可用,在 Windows 中几乎总是如此。 将 python 可执行文件添加到 PATH 变量中,或在选项中指定可执行文件路径。 有关更多详细信息,请参阅构造函数选项。

使用https://github.com/extrabacon/python-shell#pythonshellscript-options-constructor

设置 pythonPath 并尝试。

let options = {
    pythonPath: 'C:\\python27\\python',
  };

  let {PythonShell} = require('python-shell')
  PythonShell.run('test.py', options,  function  (err, results)  {
   if  (err)  throw err;
   console.log('test.py finished.');
   console.log('results', results);
  });

https://github.com/extrabacon/python-shell/issues/3#issuecomment-52174564

对于 Mac

  1. 从 python 站点安装 python 2.7。
  2. 复制目录“/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python”
  3. 打开节点模块和“dmg builder”文件夹和“dmg.js”文件替换“/usr/bin/python”。

还是一样的错误--->

  1. 升级 electron build "yarn add electron-builder --dev"

我使用了子进程,我不确定这是否适用于 python-shell

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM