如何从脚本在虚拟环境中运行程序 [英] How to run a programme inside a virtual environment from a script
问题描述
我已经在我的 Raspberry Pi 上设置了谷歌助手 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/
但是有一些变化.
我没有将 gassist.sh
作为 sudo
运行,因为它给了我以下错误:
OpenAlsaHandle PcmOpen: 没有那个文件或目录[7689:7702:ERROR:audio_input_processor.cc(756)] 输入错误ON_MUTED_CHANGED:{‘is_muted’:假}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=580:
$ cd/home/pi/env/bin$ 源激活(env) $ pip install RPi.GPIO(env) $ 停用
然后我做了sudo nano/etc/profile
并将其附加到末尾:
#Harvs 于 24/06/17 在这里如果 pidof -x "gassist.sh" >/dev/null;然后回声"回声/etc/profile 说:"echo "Google Assistant 的一个实例已经在运行,不会再次启动"回声"别的echo "正在启动 Google 助理..."回声如果你看到这个,也许你在重启后几秒钟内就拥有了 SSH"/home/pi/src/gassist.sh &菲
现在它可以完美运行,并且在虚拟环境中:)
最后我采用了以下方法:
以此为基础:https://youtu.be/ohUszBxuQA4?t=774 – 感谢 Eric Parisot
但是有一些变化.
您需要下载他使用的src文件并将其内容提取到/home/pi/src/
我没有将 gassist.sh
作为 sudo
运行,因为它给了我以下错误:
OpenAlsaHandle PcmOpen: 没有那个文件或目录[7689:7702:ERROR:audio_input_processor.cc(756)] 输入错误ON_MUTED_CHANGED:{‘is_muted’:假}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=580:
$ cd/home/pi/env/bin$ 源激活(env) $ pip install RPi.GPIO(env) $ 停用
然后我做了 sudo nano/etc/profile
并将其附加到末尾:
#Harvs 于 24/06/17 在这里如果 pidof -x "gassist.sh" >/dev/null;然后回声"回声/etc/profile 说:"echo "Google Assistant 的一个实例已经在运行,不会再次启动"回声"别的echo "正在启动 Google 助理..."回声如果你看到这个,也许你在重启后几秒钟内就拥有了 SSH"/home/pi/src/gassist.sh &菲
现在它可以完美运行,并且可以在虚拟环境中启动到 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屋!