ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

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

asked 2022-02-14 01:52:14 -0500

antoineTK gravatar image

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!

edit retag flag offensive close merge delete


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.

gvdhoorn gravatar image gvdhoorn  ( 2022-02-15 02:29:55 -0500 )edit

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= export ROS_MASTER_URI= export ROS_IP=

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


antoineTK gravatar image antoineTK  ( 2022-02-15 11:53:36 -0500 )edit

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

antoineTK gravatar image antoineTK  ( 2022-02-15 11:56:24 -0500 )edit

1 Answer

Sort by » oldest newest most voted

answered 2022-02-17 01:52:29 -0500

antoineTK gravatar image

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

edit flag offensive delete link more


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.

gvdhoorn gravatar image gvdhoorn  ( 2022-02-17 02:51:01 -0500 )edit

Question Tools



Asked: 2022-02-14 01:52:14 -0500

Seen: 160 times

Last updated: Feb 17 '22