Rosanswers logo

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 "./bosch_imu_node.py", line 42, in <module>
    import rclpy
  File "/opt/ros/dashing/lib/python3.6/site-packages/rclpy/__init__.py", line 62
    def init(*, args: List[str] = None, context: Context = None) -> None:
SyntaxError: invalid syntax

Originally posted by flynneva on ROS Answers with karma: 191 on 2019-07-28

Post score: 2

Original comments

Comment by sloretz on 2019-07-29:
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.

Comment by flynneva on 2019-07-29:
that was it! all I had to do was update the bit bang to #!/usr/bin/env python3 and it solved that error


1 Answer 1


Rosanswers logo

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.

Originally posted by flynneva with karma: 191 on 2019-07-29

This answer was ACCEPTED on the original site

Post score: 2


Your Answer

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