I'm very new to coding and software, so please stick with me. I am trying to execute a command in my Raspberry Pi terminal via a Python script. I want to be able to run this pi script from the Desktop. The command to execute is (rpi-deep-pantilt-env) pi@raspberrypi:~/rpi-deep-pantilt $ rpi-deep-pantilt detect
So as you can see, I need to cd into rpi-deep-pantilt, then activate my virtual environment, then run the command all via the py script.
Asked
Active
Viewed 51 times
0

user13930830
- 25
- 4
-
Why? Usually if you want to do something so convoluted there's a simpler way. – Pranav Hosangadi Jul 31 '20 at 19:43
1 Answers
0
A simple shell script to do what you ask:
#!/bin/sh
cd "$HOME"/rpi-deep-pantilt
. ./rpi-deep-pantilt-env/bin/activate
./rpi-deep-pantilt detect "$@"
Most or all of this is probably unnecessary. I guess you could run
#!/bin/sh
d="$HOME"/rpi-deep-pantilt
exec "$d"/rpi-deep-pantilt-env/bin/python "$d"/rpi-deep-pantilt detect "$@"
though if your Python script has hardcoded file paths which require it to run in a specific directory, that's a bug which will prevent this from working.
The "$@"
says to pass on any command-line arguments, so if you saved this script as pant
, running pant blind mice
will pass on the arguments blind
and mice
to your Python script. (Of course, if it doesn't accept additional command-line arguments after detect
, this is unimportant, but I'd still pass them on so you can generate an error message, rather than have them be ignored as if they were not there.)

tripleee
- 175,061
- 34
- 275
- 318
-
I just run those two commands in my Desktop py script and that's it? – user13930830 Jul 31 '20 at 20:49
-
That's not a Python script and there is no need to put it on the Desktop. Save it (ideally somewhere in your `PATH`) and `chmod +x` the file to make it executable; then you can run it by typing its path, like `./pant` or `/home/you/Desktop/pant` (if indeed you saved it in the `Desktop` directory and called it `pant`) or on some platforms by double-clicking its icon. See also https://stackoverflow.com/questions/8779951/how-do-i-run-a-shell-script-without-using-sh-or-bash-commands and perhaps [Difference between `./` and `~/`](https://stackoverflow.com/a/55342466/874188) – tripleee Aug 01 '20 at 08:13