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

import rclpy error invalid syntax

asked 2019-07-28 13:38:47 -0500

flynneva gravatar image

updated 2019-07-29 10:19:43 -0500

jayess gravatar image

when I try to run a simple python script to create a ROS2 node, I get an error immediately on the import rclpy line at the top.

Running on Ubuntu 18.04 Bionic, ROS2 Dashing, deb package installed in /opt/ros/dashing/ directory shown below.

I have tried building from source as well and I get the same error (just to a different directory instead).

Probably missing some dependency or something. Any advice would be greatly appreciated!

NOTE: I can successfully run the ros2 demo_nodes_py talker and listener examples.....which use import rclpy in their code....

Traceback (most recent call last):
  File "./", line 42, in <module>
    import rclpy
  File "/opt/ros/dashing/lib/python3.6/site-packages/rclpy/", line 62
    def init(*, args: List[str] = None, context: Context = None) -> None:
SyntaxError: invalid syntax
edit retag flag offensive close merge delete



What's the command used to run your python script? Without a reproducible example my best guess is the script is being run using python 2, but ROS 2 code is all python 3.

sloretz gravatar image sloretz  ( 2019-07-29 11:33:35 -0500 )edit

that was it! all I had to do was update the bit bang to #!/usr/bin/env python3 and it solved that error

flynneva gravatar image flynneva  ( 2019-07-29 19:52:44 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2019-07-29 19:53:34 -0500

flynneva gravatar image

updated 2019-07-29 19:54:23 -0500

Update for anyone else with similar issue:

fix the bit bang line to this at the top:

$ #!/usr/bin/env python3

to make sure you're using python3.

edit flag offensive delete link more

Question Tools


Asked: 2019-07-28 13:37:27 -0500

Seen: 1,447 times

Last updated: Jul 29 '19