Node.js执行Python脚本的child_process导致导入模块的错误

时间:2017-01-04 08:58:26

标签: python node.js electron child-process

使用child_process.spawn中的node.js通过电子应用程序(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应用中发生同样的情况?

1 个答案:

答案 0 :(得分:2)

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

在node.js中设置此项

DOCS)(SOURCE

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

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

如果HOMEBREW_PREFIX中没有process.env,并且知道了所需python的路径,则可以将其硬编码到spawn中:

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