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

查看:42
本文介绍了在nodejs中从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)

这是我的文件系统:

我做错了什么?

推荐答案

激活虚拟环境必须在脚本本身中完成.超出范围将不会生效.

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.

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

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