【问题标题】:Node.js child_process execution of Python script causing errors importing modulesPython脚本的Node.js child_process执行导致导入模块错误
【发布时间】:2017-05-18 12:07:45
【问题描述】:

node.js 中使用child_process.spawn 通过电子应用程序(node.js)执行python 脚本。如果我在终端(macOS)上运行 python 脚本没有问题,但是,使用node.js 和:

const spawn = require('child_process').spawn
let ls = spawn('python', ['runserver.py','--help'], {
  cwd: __dirname,
  detached: true
})

我从python 收到模块导入失败的错误,例如:

Traceback (most recent call last):
  File "runserver.py", line 17, in <module>
    from queue import Queue
ImportError: No module named queue

我已经尝试过设置不同的环境,但没有成功。

编辑:

这里是sys.path的日志:

  • 通过child_process执行代码

    [
      '/Users/xAlien95/Desktop/test.app/Contents/Resources/app',
      '/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg',
      '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
      '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
      '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
      '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
      '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
      '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
      '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
      '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
      '/Library/Python/2.7/site-packages',
      '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
      '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC'
    ]
    
  • 通过终端执行代码

    [
      '/Users/xAlien95/Desktop/test.app/Contents/Resources/app',
      '/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
      '/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
      '/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
      '/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
      '/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
      '/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
      '/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
      '/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
      '/usr/local/lib/python2.7/site-packages',
      '/usr/local/Cellar/protobuf/3.1.0/libexec/lib/python2.7/site-packages'
    ]
    

因此,在终端中它使用homebrew 下载的 Python 框架。我如何确保node.js 应用程序中也会发生同样的情况?

【问题讨论】:

  • 让 python 脚本打印/记录 sys.path。确保存在 python 安装。 stackoverflow.com/questions/897792/…
  • @StephenRauch,感谢您的建议,我编辑了问题。有没有办法在 nodejs 中轻松设置该环境?

标签: python node.js electron child-process


【解决方案1】:

让 python 找到不同库的一种方法是使用环境变量PYTHONPATH。您可以在 node.js 中使用 process.env.PYTHONPATH

进行设置

(DOCS) (SOURCE)

对于想要运行homebrew 安装的python 的特定情况,python 可执行文件应该在:

let ls = spawn(process.env.HOMEBREW_PREFIX + '/bin/python', ['runserver.py','--help'], {
    ...

如果HOMEBREW_PREFIXprocess.env 中不可用,并且所需python 的路径已知,则可以将其硬编码到spawn 中:

let ls = spawn(/*<Where my python lives>*/ + '/bin/python', ['runserver.py','--help'], {
    ...

【讨论】:

  • 不幸的是,我的 process.env 对象中没有 PYTHONPATHHOMEBREW_PREFIX。但如果我输入'/usr/local/Cellar/python/2.7.13/bin/python',它会起作用。只需使用“如果 process.env.HOMEBREW_PREFIX 不可用,只需将完整的字符串路径放在前面”编辑您的答案,我会将其标记为已回答。谢谢!
  • 嘿,所以在我的情况下,也没有 PYTHONPATH 或 HOMEBREW_PREFIX,即使使用绝对路径,我也遇到了同样的问题。我不确定我是否有正确的道路。有时我得到:spawn ENOTDIR 错误! @xAlien95 请帮忙
猜你喜欢
  • 2020-08-07
  • 2015-12-10
  • 1970-01-01
  • 1970-01-01
  • 2013-02-10
  • 2014-09-03
  • 1970-01-01
  • 2022-11-04
  • 1970-01-01
相关资源
最近更新 更多