/dev/null || export PATH="$PYENV_RO" /> /dev/null || export PATH="$PYENV_RO" /> /dev/null || export PATH="$PYENV_RO"/>

Different Python versions during bash-terminal- and crontab@reboot- and ssh-execution (while using pyenv)

65 Views Asked by At

I'm using pyenv 2.3.22 on a Raspberry Pi, with ./profile entry:

export PYENV_ROOT="$HOME/.pyenv"
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"

Since I want to execute a Python program whenever the system starts, I added a @reboot entry via crontab -e.

Everything works fine, but I get different Python runtime environments and I cannot find the reason and fix for that:

bash terminal: Python 3.11.4
crontab start: Python 3.9.2

My program is not running properly because of the differences when called by crontab.

I encapsulated the program start in a bash script to get informations for the crontab @reboot case. Everything (UserID, PATH, etc.) looks good, except the different Python versions.

I have a similar environment on annother Raspberry Pi. There it's even worse:

**bash terminal:**
python  --version.    -> 3.11.4
python3 --version.    -> 3.11.4
**crontab start:**
python  --version     -> 2.7.16
python3 --version.    -> 3.7.3

I would expect an identical Python runtime environment for both program start methods.

<added 2023-10-07> I would also be able to run the program from a remote host via ssh. It shows also a different environment that the local bash-terminal execution.

However there must be must be some differences, but I cannot find them. I would really appreciate any ideas or hints where to look.

2

There are 2 best solutions below

0
RyderHook On

Instead of starting the Python program directly, I wrote a shell script wrapper around it. This gave me insight at run time and let me execute and check the proper preparations settings (even on my Mac).

!/bin/bash
echo "startup.sh"
echo "========================"
echo "Initializing environment"
echo "------------------------"
echo "$SHELL"
myname="$(hostname -s)"
if [[ "$myname" = "MyMac" ]]
then
    echo "MacOS"
    . $HOME/.zprofile
    . $HOME/.zshrc
else
    echo "Raspberry Pi"
    . $HOME/.profile
fi
echo "$myname"
cd ~
echo "------------------------"
echo "Environment information:"
echo "------------------------"
echo "$SHELL"
whoami
echo $myname
pwd
echo "pyenv --version:"
if [[ "$myname" = "MyMac" ]]
then
    $HOME/.pyenv/bin/pyenv --version
else
    pyenv --version
fi
echo "python --version:"
python --version
echo "python3 --version:"
python3 --version
echo "-------------------"
echo "Starting Server.py:"
echo "-------------------"
date
nohup ./Server.py >./Server.log 2>&1 &

And last but not least: A big thanks to Philippe and his comment. It lead me to solving the problem.

0
Robert Betts On

In these situations, have you tried simply referencing the full absolute path the to python interpreter you want to use. Whether it’s a system wide python or a virtual environment, if you run the version of python your need, it will pull in the modules etc of its relative install. And bingo off you go.