How to launch submodules from a python package? - python

I am working on a robotics project with my lab. (Github) Most of the main components are done and documented. The setup is as follows:
We run opstn on the base station. It has a server used to communicate controller position to the robot.
We run rpi on a raspberry pi running on the robot. The two communicate via wifi and have a static address. The raspberry pi has a client program which requests the controller positions when it has time, decides what to run the motor.
The raspberry pi communicates with an mbed via USB to allow for hardware PWM.
The raspberry pi will also be working with I2C devices for monitoring data, and communicating it back to the base station for processing. This will eventually be run as a separate server process, and the base station will have a client process communicating with it.
The base station might need to have a client module to interpret the device data, running as a separate process.
The code does work, but we have to run it in a somewhat longwinded way:
On the base station, cd into kaginawa and python -m opstn, running opstn/__main__.py
SSH into the raspberry pi, cd into kaginawa and python -m rpi, running rpi/__main__.py
I was wondering how to properly launch submodules. For the rpi/__main__.py, I was thinking of providing two functions (the motor-controlling client and the device server) in __main__.py, and use multiprocessing to run them. Experiments seem to indicate that this would work.
What I want to do, though, is zip my directory into an executable, and then simply ./start_robot. This is what the Makefile does, and it works well. However, whatever I try, I seem to be unable to start opstn at all. And I will also need to work with the SSH too, since I will need to launch rpi with sudo. I can probably do that with paramiko using this reply.
If it is possible, I am thinking of using os.uname()[1] to check if it is on the raspberry pi and launch rpi, and opstn otherwise.
tl;dr How do you launch submodules that contain a __main__.py from the root __main__.py of a python package?

The toplevel directory in the project should not be a Python package. It could be a directory with setup.py instead.
You shouldn't cd inside Python package. Put your zip somewhere in PYTHONPATH (check by print(sys.path)) and use absolute names to run submodules e.g., run:
$ python -m kaginawa.opstn
to run kaginawa/opstn/__main__.py script or run:
$ python -m kaginawa.rpi
to run kaginawa/rpi/__main__.py.
If you want to choose what to run at runtime then you could call kaginawa.opstn.main() or kaginawa.rpi.main() functions in kaginawa/__main__.py. And run it:
$ python -mkaginawa
You could run the zip directly.
To simplify including dependencies, you could use PyInstaller, cx_Freeze to bundle your code.

Related

Using speedtest-cli within a Python3 script to measure internet speed - getting error message 'No module named speedtest'

My vacation home loses the internet connection from time to time which means I cannot access that locations' home network remotely. I am using a wireless Bell service as my ISP. When I am at the vacation home, I have a program within my home automation system to reboot the Bell PoE device and the Modem. This generally always resets the network.
I am trying to use the Github project Python script speedtest-cli to measure the download speed at my vacation home. I am trying to write my own Python script, on a Raspberry Pi (Buster), to periodically measure the download speed of the internet. My plan is to then send a variable, with the speed, to my home automation system that will trigger a reset of the internet if the speed is too low.
I downloaded the latest script from Github using the code
wget -O speedtest-cli https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py
Then I made the file executable with
chmod +x speedtest-cli
When I run the command speedtest-cli, at the Raspeberry Pi's command line, the script works as expected and I see the download speed.
However, when I try to incorporate the code into a python script I get the error 'No module named 'speedtest'
The simple code that yields this message is
import speedtest
I am running the simple script above from the /home/pi/ directory and the executable 'speedtest-cli' is also in that directory
(I know that there are numerous comments that this 'speedtest' project isn't providing accurate results, however from my testing it has been consistently yielding results that are sufficient for my purposes)
Any thought on why this isn't working?
For using speedtest-cli via a python script, would require you to install speedtest-cli on your local machine.
git clone https://github.com/sivel/speedtest-cli.git
cd speedtest-cli
python setup.py install
After that you can use import speedtest

Unable to start when bootup of raspberry pi a .py program

I have been struggling for a while trying to make a .py file start on a raspberry pi bootup. the file includes opencv, tensorflow and picamera usage. When i try to start the program at boot using different methods it wont start.
The methods i've tried are:
/etc/rc.local (both trying to start the program directly or a .sh script that boots the program itself)
/home/pi/.config/autostart (either starting directly the program with "Exec= '/usr/bin/python3 /pathtoprogram.py') (or using an extern program called "xterm" to try and boot up the program but it fails to start the program)
.bashrc with (sudo python3 /pathtoprogram.py)
I'm atually looking for new methods. the program boots perfectly when i start it myself on the raspberry pi.
Additional info:
my program is a modified version of this one: https://www.tomshardware.com/how-to/raspberry-pi-facial-recognition
using the picam version.
Try systemd:
https://www.raspberrypi.org/documentation/linux/usage/systemd.md
"In order to have a command or program run when the Pi boots, you can add it as a service. Once this is done, you can start/stop enable/disable from the linux prompt."

Set up a Raspberry Pi 3B+ to boot up and run a Python script immediately

Initial conditions: Raspberry Pi 3B running on latest Raspbian OS
Application: Python script recording audio through a USB Microphone and lighting up LEDS on the GPIOs ports.
Currently working: We need to wait for the Raspberry Pi OS fully up and running and then we launch the Python script from the command line.
What we need: As soon as we power up the Raspberry, we would like the unit to boot up and run our Python script directly without any extra UI input.
There are quite a few ways, see here. One of the ways would be to add the program to be run at startup to the init.d directory using the following lines:
sudo cp /home/pi/sample.py /etc/init.d/
Move to the init directory and open the sample script
cd /etc/init.d
sudo nano sample.py

How to execute python script based on the server of an ssh connection?

I'm connected to a Raspberry Pi via ssh. I'm trying to run a script on the RPi but rather than using a nano editor on the terminal I want to execute a python script based on my mac (the server of the SSH).
However when typing
'sudo python /Users/User/Pythonscript.py'
terminal returns
'python: can't open file '/Users/User/Pythonscript.py': [Errno 2] No such file or directory'
And yet this file does exist under that directory.
Any ideas?
Ok, if I have properly understood, you have script on your client and want to execute it on Pi from ssh.
scp /path/to/script.py user#hostname:/path/to/
then add your rsa key to your server. This perfect guide tells you how.
Then just write .sh, which get access to your server ssh user#hostname, then cd /path/to and, finally python script.py
You have a file on your Mac and want to execute it on your Pi. Two things need to be done: First, get the file to the Pi. Second log in to the Pi and run it. Apparently, you managed step two, so I'll address step 1.
The simple solution: scp, e.g. scp /Users/User/Pythonscript.py <user>#<ip_of_pi>:<target_dir>
The solution that might be longer-term more feasible, if you want to develop locally: sshfs. It's available via Homebrew. You mount a directory locally, and anything you change will automatically be reflected on the respective directory on the Pi. Here's a tutorial how to install and use sshfs. On first glance, it seems reasonable.
No matter how you get your script to the Pi, you need to find it on the Pi and execute it there.

Script to run at startup on Windows RT

I have a python script which basically lauches an Xmlrpc server. I need this script to run always. I have a function that may call for the system to reboot itself. So once the system has rebooted, I need to get the script running again.
How can I add this to the Windows RT startup?
There is no way for a Windows Store app to trigger a system reboot, neither can it run a Python script unless you implement a Python interperter inside your app. Windows Store apps run in their own sandbox and have very limited means of communication with the rest of the system.
You can create a scheduled task to run on login or boot. The run registry key and the startup folder do not function on Windows RT, but the task scheduler does.
Off topic (since I seem unable to add a comment to the other answer) there has been a copy of Python ported over to Windows RT using the jailbreak.

Categories