如何通过脚本在虚拟环境中运行程序 [英] How to run a programme inside a virtual environment from a script

查看:79
本文介绍了如何通过脚本在虚拟环境中运行程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Raspberry Pi上设置了Google助手sdk,如下所示: https://developers.google.com/assistant/sdk/prototype/getting-started-pi-python/run-sample

现在为了重新运行助手,我已经计算出两个命令

  $ source env/bin/activate 

 (env)$ google-assistant-demo 

但是我想将此过程自动化为一个脚本,我可以从 rc.local 调用该脚本(后接& ),以使助手从以下位置启动启动.

但是如果我运行一个简单的脚本

 #!/bin/bash源环境/bin/激活谷歌助手演示 

该助手不在环境中运行我的环境路径是/home/pi/env/bin/activate 我怎么拥有它,以便脚本启动环境,然后在虚拟环境内 内运行助手?

最后,我使用以下方法:

以此为基础: https://youtu.be/ohUszBxuQA4?t=774 –感谢Eric Parisot

您将需要下载src文件,并将其内容提取到/home/pi/src/

但是有一些更改.

我没有以 sudo 的身份运行 gassist.sh ,因为它给了我以下错误:

  OpenAlsaHandle PcmOpen:无此类文件或目录[7689:7702:ERROR:audio_input_processor.cc(756)]输入错误ON_MUTED_CHANGED:{‘is_muted’:False}ON_START_FINISHEDON_ASSISTANT_ERROR:{‘is_fatal’:正确}[7689:7704:ERROR:audio_input_processor.cc(756)]输入错误ON_ASSISTANT_ERROR:{‘is_fatal’:正确} 

修复:请勿以 sudo

的身份运行

如果 gassist.sh 给出有关 RPi.GPIO 的错误,则需要执行

现在,它可以在虚拟环境中完美运行:)

最后,我使用了以下方法:

以此为基础: https://youtu.be/ohUszBxuQA4?t=774 –感谢Eric Parisot

但是有一些更改.

您将需要下载src文件,并将其内容提取到/home/pi/src/

我没有以 sudo 的身份运行 gassist.sh ,因为它给了我以下错误:

  OpenAlsaHandle PcmOpen:无此类文件或目录[7689:7702:ERROR:audio_input_processor.cc(756)]输入错误ON_MUTED_CHANGED:{‘is_muted’:False}ON_START_FINISHEDON_ASSISTANT_ERROR:{‘is_fatal’:正确}[7689:7704:ERROR:audio_input_processor.cc(756)]输入错误ON_ASSISTANT_ERROR:{‘is_fatal’:正确} 

修复:请勿以sudo身份运行

如果 gassist.sh 给出有关 RPi.GPIO 的错误,则需要执行

现在,它可以在虚拟环境中完美运行,并且可以引导至CLI模式!:)

I have set up the google assistant sdk on my Raspberry Pi as shown here: https://developers.google.com/assistant/sdk/prototype/getting-started-pi-python/run-sample

Now in order to re-run the assistant I have worked out the two commands are

$ source env/bin/activate

and

(env) $ google-assistant-demo

however I want to automate this process into a script that I can call from rc.local (followed by an &) in order to make the assistant boot from start up.

However if I run a simple script

#!/bin/bash
source env/bin/activate
google-assistant-demo

the assistant does not run inside the environment my environment path is /home/pi/env/bin/activate How can I have it so the script starts the environment and then runs the assistant inside the virtual environment?

EDIT: In the end I went with the following method:

using this as a base : https://youtu.be/ohUszBxuQA4?t=774 – thanks to Eric Parisot

You will need to download the src file he uses and extract its contents into /home/pi/src/

However with a few changes.

I did not run gassist.sh as sudo, as it gave me the following error:

OpenAlsaHandle PcmOpen: No such file or directory
[7689:7702:ERROR:audio_input_processor.cc(756)] Input error
ON_MUTED_CHANGED:
{‘is_muted’: False}
ON_START_FINISHED
ON_ASSISTANT_ERROR:
{‘is_fatal’: True}
[7689:7704:ERROR:audio_input_processor.cc(756)] Input error
ON_ASSISTANT_ERROR:
{‘is_fatal’: True}

Fix: DO NOT run as sudo

If gassist.sh gives an error about RPi.GPIO you need to do https://youtu.be/ohUszBxuQA4?t=580:

$ cd /home/pi/env/bin
$ source activate
(env) $ pip install RPi.GPIO
(env) $ deactivate

And then I did sudo nano /etc/profile and the appended this to the end:

#Harvs was here on 24/06/17
if pidof -x "gassist.sh" >/dev/null; then
    echo ""
    echo "/etc/profile says:"
    echo "An instance of Google Assistant is already running, will not start again"
    echo ""
else
    echo "Starting Google Assistant..."
    echo "If you are seeing this, perhaps you have SSH within seconds of reboot"
    /home/pi/src/gassist.sh &
fi

And now it works perfectly, and inside the virtual enviroment :)

解决方案

In the end I went with the following method:

using this as a base : https://youtu.be/ohUszBxuQA4?t=774 – thanks to Eric Parisot

However with a few changes.

You will need to download the src file he uses and extract its contents into /home/pi/src/

I did not run gassist.sh as sudo, as it gave me the following error:

OpenAlsaHandle PcmOpen: No such file or directory
[7689:7702:ERROR:audio_input_processor.cc(756)] Input error
ON_MUTED_CHANGED:
{‘is_muted’: False}
ON_START_FINISHED
ON_ASSISTANT_ERROR:
{‘is_fatal’: True}
[7689:7704:ERROR:audio_input_processor.cc(756)] Input error
ON_ASSISTANT_ERROR:
{‘is_fatal’: True}

Fix: DO NOT run as sudo

If gassist.sh gives an error about RPi.GPIO you need to do https://youtu.be/ohUszBxuQA4?t=580:

$ cd /home/pi/env/bin
$ source activate
(env) $ pip install RPi.GPIO
(env) $ deactivate

And then I did sudo nano /etc/profile and the appended this to the end:

#Harvs was here on 24/06/17
if pidof -x "gassist.sh" >/dev/null; then
    echo ""
    echo "/etc/profile says:"
    echo "An instance of Google Assistant is already running, will not start again"
    echo ""
else
    echo "Starting Google Assistant..."
    echo "If you are seeing this, perhaps you have SSH within seconds of reboot"
    /home/pi/src/gassist.sh &
fi

And now it works perfectly, and inside the virtual enviroment, and in boot to CLI mode! :)

这篇关于如何通过脚本在虚拟环境中运行程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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