shell script works, but not on boot. [Raspberry pi]

Question:

I have a python script running on a raspberry pi.

I defined a launcher.sh file and trying it with sh launcher.sh runs the program

The file

cd /home/pi/Documents
PYTHONPATH=/home/pi/.local/lib/python3.10/site-packages
python3 /home/pi/Documents/myfile.py

I tried chmod launcher.sh and sudo chmod 775 launcher.sh

But when I start the launcher on boot, I get an error loged in cronlog:

import pandas as pd ModuleNotFoundError No module named pandas

I do have two python versions on the Raspberry 3.9 and 3.10 (unfortunately not in an environment).

pip list gives me: pandas 1.4.3

python --version gives me Python 3.10.0

python3 --version also gives me Python 3.10.0

In /home/pi/.local/lib/ I do have a python 3.10 and python 3.9 folder. Both have a pandas folder in the sitepackages.I just saw, that the pyhton3.9 folder also has a pandas-1.4.3dist.info folder (The python3.10 does not have that).

The crontab line is as follows:

@reboot sh /home/pi/launcher.sh >/home/pi/logs/cronlog 2>&1

I don’t get it…
is the python 3.9 folder somehow intercepting… But I do set the Pythonpath and trying to run with sh launcher.sh does work, so I wonder what is going wrong. Somehow the wrong python is used I guess.

Thanks for your help

Edit:
I renamed the /home/pi/.local/lib/python3.9 to get rid of the sidpackage… but no change

Edit: I tried to change the priority of python 3.10 and 3.9 with

sudo update-alternatives --install $(which python) python $(readlink -f $(which python3.10)) 3
sudo update-alternatives --install $(which python) python $(readlink -f $(which python3.9)) 2

with no luck

Edit: added the python3 –version suggestion

Edit: Error message when trying to install pandas for python3.9
with pyhton3.9 -m pip install pandas See output below.

…………almost FINAL EDIT:…………

I deinstalled (purge) python like suggested here and then reinstalled python3.10 via this great link
Unfortunattely while deinstalling, I guess also the python2.7 was deinstalled, resulting in a complete system failure on startup. -> Lost all hope at 3 o’clock at night 😉 And now resetting the sd-image starting all over.

I will update a last time when I got it running with the fresh image

Thank you all so much for your interest and participation. I love this community

……………………………………..

> ERROR: Exception: Traceback (most recent call last):   File
> "/usr/lib/python3/dist-packages/pip/_internal/cli/base_command.py",
> line 223, in _main
>     status = self.run(options, args)   File "/usr/lib/python3/dist-packages/pip/_internal/cli/req_command.py",
> line 180, in wrapper
>     return func(self, options, args)   File "/usr/lib/python3/dist-packages/pip/_internal/commands/install.py",
> line 297, in run
>     session = self.get_default_session(options)   File "/usr/lib/python3/dist-packages/pip/_internal/cli/req_command.py",
> line 78, in get_default_session
>     self._session = self.enter_context(self._build_session(options))   File
> "/usr/lib/python3/dist-packages/pip/_internal/cli/req_command.py",
> line 88, in _build_session
>     session = PipSession(   File "/usr/lib/python3/dist-packages/pip/_internal/network/session.py",
> line 248, in __init__
>     self.headers["User-Agent"] = user_agent()   File "/usr/lib/python3/dist-packages/pip/_internal/network/session.py",
> line 131, in user_agent
>     zip(["name", "version", "id"], distro.linux_distribution()),   File
> "/usr/share/python-wheels/distro-1.5.0-py2.py3-none-any.whl/distro.py",
> line 125, in linux_distribution
>     return _distro.linux_distribution(full_distribution_name)   File "/usr/share/python-wheels/distro-1.5.0-py2.py3-none-any.whl/distro.py",
> line 681, in linux_distribution
>     self.version(),   File "/usr/share/python-wheels/distro-1.5.0-py2.py3-none-any.whl/distro.py",
> line 741, in version
>     self.lsb_release_attr('release'),   File "/usr/share/python-wheels/distro-1.5.0-py2.py3-none-any.whl/distro.py",
> line 903, in lsb_release_attr
>     return self._lsb_release_info.get(attribute, '')   File "/usr/share/python-wheels/distro-1.5.0-py2.py3-none-any.whl/distro.py",
> line 556, in __get__
>     ret = obj.__dict__[self._fname] = self._f(obj)   File "/usr/share/python-wheels/distro-1.5.0-py2.py3-none-any.whl/distro.py",
> line 1014, in _lsb_release_info
>     stdout = subprocess.check_output(cmd, stderr=devnull)   File "/usr/lib/python3.9/subprocess.py", line 424, in check_output
>     return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,   File "/usr/lib/python3.9/subprocess.py", line 528, in run
>     raise CalledProcessError(retcode, process.args, subprocess.CalledProcessError: Command '('lsb_release', '-a')'
> returned non-zero exit status 1.
Asked By: Florida Man

||

Answers:

The answer is that contab runs applications as root and therefore the packages have to be installed with sudo

sudo pip install pandas 

solved the problem.
Shoutout to CoderMike

Answered By: Florida Man

I don’t see a shabang in your script.

Usually scripts being executed through the cli require

execution permission : chmod +x
shabang at the beginning of the file.

#!/usr/bin/env bash 
cd /home/pi/Documents
PYTHONPATH=/home/pi/.local/lib/python3.10/site-packages
python3 /home/pi/Documents/myfile.py

Hope this helps.

Answered By: maxLeet