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

import rclpy without sourcing setup.bash

asked 2021-04-07 09:19:45 -0500

highmax1234 gravatar image


I want to use the rclpy module in a python script. This script is called by a flask application which is invoked by an apache2 webserver and does not get the necessary bash context. When I run the python script from bash with python3 everything works fine, as I have . /opt/ros/foxy/setup.bash in my ~/.bashrc .

When the script gets invoked by flask and apache2, import rclpy is not found. Exeucting sys.path.insert("/opt/ros/foxy/lib/python3.8/site-packages/") brings another error:

The C extension '/opt/ros/foxy/lib/python3.8/site-packages/rclpy/' failed to be imported while being present on the system. Please refer to '' for possible solutions

There has been a similar Thread with no solution. The essence seems to be that opt/ros/foxy/setup.bash does more than just setting paths.

Now, is there any possibilty to use rclpy in a python script without sourcing from bash? Thanks

Ubuntu 20.04
ROS2 foxy

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2021-07-02 04:31:33 -0500

As part of my GSoC project, I had researched and resolved the same problem. My findings and solutions are mentioned in my GSoC blog post

For completeness, I will cover the main points here.
When you invoke the script via flask and apache2, the ~/.bashrc does get sourced but suffers an early exit due to the following lines-

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

You're running it using a non-interactive terminal. Thus "$PS1" is not set.

The bash script you mentioned in the other answer is quite acceptable but it's useful to know the root of the problem. I have included more links in my blog post if you'd like to understand this better.

edit flag offensive delete link more

answered 2021-05-11 10:03:52 -0500

highmax1234 gravatar image

I solved it by not invoking the python script from flask, but by calling a bash script from flask.

source /opt/ros/foxy/setup.bash
edit flag offensive delete link more


I think the problem you were running-into was that only upon user-login will your ~/.bashrc file be sourced, and therefore source /opt/ros/foxy/setup.bash never runs. From what I've seen in similar settings involving Bamboo and Jenkins your new approach is correct. I think it also has to do with new terminal sessions and how you must source scripts/etc. for each new session. Someone more knowledgeable might have some ideas involving the export command from Bash; there might be something you can do with that.

Spectre gravatar image Spectre  ( 2021-05-11 14:39:33 -0500 )edit

I tried exporting, but it is not enough for sourcing ros. The setup.bash does more than setting env variables. I did some unsucssessfull experiments with os.env.

highmax1234 gravatar image highmax1234  ( 2021-05-11 15:23:12 -0500 )edit

Question Tools



Asked: 2021-04-07 09:19:45 -0500

Seen: 2,118 times

Last updated: Jul 02 '21