rclpy import in application fails

asked 2018-10-23 04:42:53 -0500

bhmth gravatar image

updated 2018-10-25 07:20:47 -0500


i trying to import the rclpy libary in an application written in python.

if i use #import rclpy i alway get an error message like this

ImportError: DLL load failed: ...

The C extension 'C:\ros2\install\Lib\site-packages\rclpy_rclpy.pyd' failed to be imported while being present on the system. Please refer to 'https://github.com/ros2/ros2/wiki/Rclpy-Import-error-hint' for possible solutions

But when i run the demo_nodes_py everything seems to work fine

I dont want to build the python package with ament or colcon i just want to use the rclpy API is this possible ?

edit: it works with this parent script

def main():
    # extend the path first  by prepending ros2 path settings
    path = os.environ.get('PATH').split(os.pathsep)
    path = [os.path.join(ROS_INSTALL_DIR, 'Scripts'),  os.path.join(ROS_INSTALL_DIR, 'bin')] + path
    os.environ['PATH'] = os.pathsep.join(path)

    # extend the pythonpath with the ros2 site packages
    pythonpath = os.environ.get('PYTHONPATH').split(os.pathsep)
    pythonpath = ['C:\\ros2\\install\\Lib\\site-packages\\'] + pythonpath
    os.environ['PYTHONPATH'] = os.pathsep.join(pythonpath)

    # spawn a python process with extended environment
    subprocess.check_call([sys.executable, SCRIPT], env=dict(os.environ))
edit retag flag offensive close merge delete


How are you getting ROS (from a .zip or did you build it yourself)? It looks like you're maybe using a debug build (given the .pyd) are you using python_d?

William gravatar image William  ( 2018-10-23 18:37:51 -0500 )edit

i installed ROS2(bouncy) from source and using a normal python3.7 release on Win10

bhmth gravatar image bhmth  ( 2018-10-24 09:05:24 -0500 )edit

Did you source the setup.bat file? What is your PYTHONPATH and PATH look like?

William gravatar image William  ( 2018-10-25 00:04:48 -0500 )edit

BTW, I was wrong about pyd, that's just a dll not necessarily a debug build.

William gravatar image William  ( 2018-10-25 00:05:59 -0500 )edit

so i can get it to work with the local_setup.bat but when i try to manually extend the PYTHONPATH and PATH to use the rclpy API it always fails

bhmth gravatar image bhmth  ( 2018-10-25 05:43:32 -0500 )edit

You should always be using the setup script because it sets other things like environment variables for middlewares and standard things like ROS_DOMAIN_ID, but without seeing the difference in your Python path and the one the setup script sets I can't really help.

William gravatar image William  ( 2018-10-25 14:55:17 -0500 )edit