How to run a programme inside a virtual environment from a script

一个人想着一个人 提交于 2020-01-03 04:53:09

问题


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 :)


回答1:


found solution here :https://raspberrypi.stackexchange.com/a/45089

Create a startup shell script in your root directory (I named mine "launch"), make it executable too :

sudo nano launch.sh

I wrote it that way :

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

Save the file

Edit the LXDE-pi autostart file

sudo nano /home/pi/.config/lxsession/LXDE-pi/autostart

Add this to the bottom of that file

./launch.sh

reboot




回答2:


I think this post might be helpful: Why do people write #!/usr/bin/env python on the first line of a Python script? Basically, yo would be adding #!/usr/bin/env python at the top of your script instead of #!/bin/bash, and you would be making your file executable with chmod 755. Also do not forget to keep the & at the end of the call google-assistant-demo &, and to reference absolute filenames rather than relative ones as stated in https://www.raspberrypi.org/documentation/linux/usage/rc-local.md




回答3:


Scripts run from rc.local execute in the root directory (or possibly in the home directory of the root user, depending on the distro, I think?)

The easy fix is to code the full path to the environment.

#!/bin/bash
source /home/pi/env/bin/activate
google-assistant-demo
# or maybe /home/pi/google-assistant-demo

There is no need to explicitly background anything in rc.local




回答4:


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! :)



来源:https://stackoverflow.com/questions/44230522/how-to-run-a-programme-inside-a-virtual-environment-from-a-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!