Search code examples
pythonboot

Delay execution of Python script after system start up


I've got a Python script that I need to run upon start up and problem is that it throws an error saying "no module named xyz". I'm using external library which I installed using pip3. The script works just fine on its own but I get aforementioned error when I want to run it right after boot.

What should I do ? I tried to delay importing the library with time.sleep(10) in case third-party libraries need few more seconds to load up after boot, but that didn't have desired effect.

I run the script on Raspberry Pi with Debian-based os called Raspbian. I configured execution upon start up by adding this sudo python3 script.py into /etc/profile file.


Solution

  • I don't see how pip install without sudo could have worked.

    What I see: scripts run on startup from cron or /etc/profile are run under root, not under pi user. Thus, they don't have the same $PATH, $PYTHONPATH and other environment variable values that you have in your user's shell.

    As pip install managed to run without sudo, I suspect that you installed your module into a user-specific directory, which is not a part of root's Python environment.

    Replacing the /etc/profile line with sudo -u pi python3 script.py may help.

    Whatever it is, it's the difference that is already in the Python environment. Waiting for 10s "for whatever to come up" will not help it.