【发布时间】: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