Robotics StackExchange | Archived questions

Node looking for python2 lib when using #!/usr/bin/env python3 shebang

Hello all,

I am using melodic and putting the python 3 shebang works when starting node manually. The issue is when I try to have it start automatically at boot.

I have a cron job starting a bash script on boot. Every other node starts fine but the only python 3 node I start is failing with

ModuleNotFoundError: No modules named 'sklearn'

Which is installed for python 3 but not for python 2.

Again, this works perfectly starting the node manually from terminal.

Any idea?

Thank you!

Asked by antoineTK on 2022-02-14 02:52:14 UTC

Comments

I'm wondering whether you can draw the conclusion you draw in your post about the node "looking for python2".

Cron by default does not use a regular user to start your jobs. If something like PYTHONPATH is not configured, your script will not run correctly. That would be caused by using a different Python version.

Again, this works perfectly starting the node manually from terminal.

which could be explained by you using a regular user to start your script.

Asked by gvdhoorn on 2022-02-15 03:29:55 UTC

the node "looking for python2" comment is my best guess to what is happening, but you are right it could be something else

This is what I do in the bash script that is started from the cron job

source /opt/ros/melodic/setup.bash source /home/nvidia/ROS/robot_ws/devel/setup.bash

export ROS_HOSTNAME=127.0.0.1 export ROS_MASTER_URI=http://127.0.0.1:11311 export ROS_IP=192.168.1.2

export ROS_LOG_DIR="/home/nvidia/Desktop/Logs/ROS_logs"

export ROS_PYTHON_VERSION=3

Asked by antoineTK on 2022-02-15 12:53:36 UTC

if I echo $PYTHONPATH in the bash script and log the output to a log file, I have the same output as if I enter the command by hand in the terminal

the output only references python 2.7, but my python3 node does work from a terminal window

Asked by antoineTK on 2022-02-15 12:56:24 UTC

Answers

so I ended up adding

import sys

sys.path.insert(0, '/home/nvidia/.local/lib/python3.6/site-packages')

to the top of my python3 script, which explicitly links to where pip3 installs sklearn. Seems to work

Asked by antoineTK on 2022-02-17 02:52:29 UTC

Comments

This is exactly what I was referring to.

For the user Cron uses to run your script, the Python search path is not setup correctly.

Note that sys.path.insert(..) is really a work-around.

Personally I would try to see whether using the correct user (if you aren't already) for Cron (ie: a per-user crontab) fixes it properly.

But if you're happy, we're happy.

Asked by gvdhoorn on 2022-02-17 03:51:01 UTC