Ask Your Question
1

import rclpy without sourcing setup.bash

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

highmax1234 gravatar image

Hi,

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 foo.py 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/_rclpy.cpython-38-x86_64-linux-gnu.so' failed to be imported while being present on the system. Please refer to 'https://index.ros.org/doc/ros2/Troubleshooting/Installation-Troubleshooting/#import-failing-even-with-library-present-on-the-system' 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
1

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

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
0

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

highmax1234 gravatar image

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

#!/bin/bash
source /opt/ros/foxy/setup.bash
python3 foo_containing_ros2_node.py
edit flag offensive delete link more

Comments

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 -0600 )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 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

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

Seen: 343 times

Last updated: Jul 02 '21