在Node.js中从Python激活虚拟环境 [英] Activating an virtual environment from python in nodejs

查看:307
本文介绍了在Node.js中从Python激活虚拟环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在树莓派4上有以下项目: 我用python创建了一个面部识别脚本,该脚本需要虚拟环境才能运行. 该脚本将打印出被检测到的人.

I have following project on a raspberry pi 4: I created a face recognition script in python which needs an virtual enviroment to run. The script prints out the person which has been detected.

在NodeJS中,我想通过在节点(最小版本)中运行脚本来接收答案:

In NodeJS I want to receive the answer by running the script in node (minified version):

const http = require("http");
const server = http.createServer((req, res) => {

var spawn = require('child_process').spawn,
py    = spawn('python', ['faceReg.py'],)
py.stdout.on('data', function(data){
  console.log('Data:' + data);

});
py.stdout.on('end', function(){
  console.log('Python ended');


 });

});

在执行代码时,我立即得到一个"python结尾".

When executing the code I get immdeaitly an "python ended".

在我的pi上,我可以在执行之前运行以下命令来运行脚本:

On my pi I can run the script when I run following command before execution:

source ~/.virtualenvs/cv2_env/bin/activate 

python脚本基本上是:

The python script is basicly:

stop = False
while(stop==False):
   print("Peter")

更新

运行时

py    = spawn('~/.virtualenvs/cv2_env/bin/python', ['faceReg.py'])

我收到以下错误:

events.js:174
      throw er; // Unhandled 'error' event
      ^
Error: spawn ~/.virtualenvs/cv2_env/bin/python 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:757:11)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622: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:622:3)

这是我的文件系统:

This is my file system:

我在做什么错了?

推荐答案

激活虚拟环境必须在脚本本身中完成.否则将不会生效.

Activating the virtual environment must be done in the script itself. Outside of that will not take effect.

类似于答案此处,您只想执行虚拟环境中列出的Python运行时.该问题引用了pip,但实际上您可以将其替换为所需的Python版本(存在于虚拟环境中).换句话说,您将替换以下行:

Similar to the answer here, you'll just want to execute the Python runtime that's listed in your virtual environment. That question references pip but you can essentially just replace it with the version of Python you want (that exists in your virtual environment). In other words, you would replace this line:

py    = spawn('python', ['faceReg.py'],)

此行:

py    = spawn('/home/youruser/.virtualenvs/cv2_env/bin/python', ['faceReg.py'],)

注意:如果失败,则可能需要将/home/youruser/.virtualenvs/cv2_env/bin/python更改为要查找的Python版本,例如/home/youruser/.virtualenvs/cv2_env/bin/python3/home/youruser/.virtualenvs/cv2_env/bin/python3.7.

Note: If this fails, you may need to change /home/youruser/.virtualenvs/cv2_env/bin/python to be whatever version of Python you're looking for e.g. /home/youruser/.virtualenvs/cv2_env/bin/python3 or /home/youruser/.virtualenvs/cv2_env/bin/python3.7.

这篇关于在Node.js中从Python激活虚拟环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆