0
$\begingroup$

Rosanswers logo

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


Originally posted by highmax1234 on ROS Answers with karma: 98 on 2021-04-07

Post score: 1

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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.


Originally posted by trunc8 with karma: 156 on 2021-07-02

This answer was ACCEPTED on the original site

Post score: 1

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.