简体   繁体   English

在Node.js中从Python激活虚拟环境

[英]Activating an virtual environment from python in nodejs

I have following project on a raspberry pi 4: I created a face recognition script in python which needs an virtual enviroment to run. 我在树莓派4上有以下项目:我在python中创建了一个面部识别脚本,该脚本需要虚拟环境才能运行。 The script prints out the person which has been detected. 该脚本将打印出已检测到的人。

In NodeJS I want to receive the answer by running the script in node (minified version): 在NodeJS中,我想通过在节点(最小版本)中运行脚本来接收答案:

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');


 });

});

When executing the code I get immdeaitly an "python ended". 当执行代码时,我立即感到“ python结束”。

On my pi I can run the script when I run following command before execution: 在我的pi上,我可以在执行之前运行以下命令来运行脚本:

source ~/.virtualenvs/cv2_env/bin/activate 

The python script is basicly: python脚本基本上是:

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

Update 更新资料

When running 跑步时

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

I get following error: 我收到以下错误:

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: 这是我的文件系统: 在此处输入图片说明

What am I doing wrong? 我究竟做错了什么?

Activating the virtual environment must be done in the script itself. 激活虚拟环境必须在脚本本身中完成。 Outside of that will not take effect. 否则将不会生效。

Similar to the answer here , you'll just want to execute the Python runtime that's listed in your virtual environment. 这里的答案类似,您只需要执行虚拟环境中列出的Python运行时。 That question references pip but you can essentially just replace it with the version of Python you want (that exists in your virtual environment). 该问题引用了pip但您基本上可以将其替换为所需的Python版本(存在于虚拟环境中)。 In other words, you would replace this line: 换句话说,您将替换以下行:

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

With this line: 用这行:

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

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 eg /home/youruser/.virtualenvs/cv2_env/bin/python3 or /home/youruser/.virtualenvs/cv2_env/bin/python3.7 . 注意:如果失败,则可能需要将/home/youruser/.virtualenvs/cv2_env/bin/python更改为要查找的Python版本,例如/home/youruser/.virtualenvs/cv2_env/bin/python3/home/youruser/.virtualenvs/cv2_env/bin/python3.7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM