Create a Python package in ROS2 step by step
Atlasbot · 17/8/2026 · 10 min read
In parallel with lesson Create C++ package, this is the equivalent process for ament_python — a popular package type for high-level logic processing nodes (state machine, API calls, image processing with OpenCV) where development speed is more important than pure performance.
Key concepts
cd ~/ros2_ws/src
ros2 pkg create --build-type ament_python my_py_pkg \
--dependencies rclpy std_msgs
The generated structure is different from C++ in that there are setup.py/setup.cfg instead of CMakeLists.txt, and the code directory has the same package name instead of src/:
my_py_pkg/
├── package.xml
├── setup.py ← installation declaration + executable
├── setup.cfg
├── resource/my_py_pkg ← package marker file (ament index)
└── my_py_pkg/
└── __init__.py
How it works
Create my_py_pkg/hello_node.py:
import rclpy
from rclpy.node import Node
def main(args=None):
rclpy.init(args=args)
node = Node("hello_node")
node.get_logger().info("hello_node đã khởi động")
rclpy.spin(node)
rclpy.shutdown()
if __name__ == "__main__":
main()
Different from C++ (declared executable in CMakeLists.txt), Python declared in setup.py, section entry_points:
entry_points={
"console_scripts": [
"hello_node = my_py_pkg.hello_node:main",
],
},
Syntax "command_name = module.file:function" — the left side is the name you type after ros2 run my_py_pkg, the right side is the actual Python path to the function main() to call.
In short:
entry_pointsinsetup.pyis the "CMakeLists.txt of the Python world" — both answer the same question: which file/function executesros2 run <package> <name>.
Build and run — symlink-install tip:
cd ~/ros2_ws
colcon build --packages-select my_py_pkg --symlink-install
source install/setup.bash
ros2 run my_py_pkg hello_node
Flag --symlink-install only makes sense for Python packages: instead of copying file .py Input install/, colcon Create a symlink pointing back src/. The result is finished editing the Python code can run again immediately, no need colcon build again.
Technical details
Parameters
Some parameters/conventions to know when working with Python packages in ROS2:
| Parameters | Value |
|---|---|
| Python version according to distro | Humble/Iron: Python 3.10 · Jazzy: Python 3.12 |
| The actual installation directory of node | install/<pkg>/lib/<pkg>/<executable_name> |
| Required markup file | resource/<package_name> (empty, just to let ament identify the package that exists) |
zip_safe trong setup.py | Should be set to False — avoid setuptools compressing package into file .egg, causing error when ROS2 needs to read files directly in the |
A Python package can also declare multiple executables at the same time in entry_points, each line is an independent program that can be run in ros2 run:
entry_points={
"console_scripts": [
"hello_node = my_py_pkg.hello_node:main",
"diagnostic_tool = my_py_pkg.diagnostic_tool:main",
],
},
Practical applications
A common practical development process is prototype in Python first, port to C++ later if necessary: write test algorithm logic (eg, a new obstacle avoidance strategy) in Python thanks to fast iteration speed (fix code, run again immediately thanks to --symlink-install, no need to wait for compilation), after confirming correct logic and need to optimize performance (high frequency running, latency low), just rewrote that "hot path" part in C++. This approach takes advantage of the right strengths of each language at the right stage of development — many robot packages actually write configuration/interface nodes in Python (e.g., reading complex YAML files, calling external APIs) in parallel with high-frequency data processing nodes in C++ in the same workspace, eliminating the need to choose a single language for the entire project.
Practical example
$ ros2 run my_py_pkg hello_node
Output:
[INFO] [1723000000.111] [hello_node]: hello_node đã khởi động
# Edit log line in hello_node.py, SAVE FILE, do not rebuild
$ ros2 run my_py_pkg hello_node
Output (changes take effect immediately, no build colcon needed):
[INFO] [1723000005.222] [hello_node]: hello_node started (new version)
This is the real benefit of --symlink-install — the edit-run-test loop is significantly shortened compared to having to rebuild (albeit quickly) only changing 1 line of Python code at a time.
Common mistakes
- Forgot
--symlink-installand wonder why editing the code doesn't work — if the build doesn't use this flag,install/contains a static copy of the code at build time — editing the source file insrc/then won't automatically update, must be rebuilt each time. - Missing file marked
resource/<package_name>— if this file was deleted by mistake or the name does not exactly match the package name,amentwill not recognize the package as existing even if all other files are correct. - Incorrect syntax
entry_points— spelling error in pathmodule.file:function(eg: wrong file or function name) causesros2 runto report "no executable found" even though the package build was successful. - Mixing tabs and spaces in
setup.py— Python is sensitive to indentation, errorIndentationErrordue to mixing tabs/spaces is a common syntax error when copying code from many different sources into the same file.
Conclusion
Package Python is simpler than C++ in that it does not require compilation, but still requires colcon build to install in the correct location — entry_points in setup.py acts as equivalent to CMakeLists.txt. Use --symlink-install for the fastest growing loop. The next article in this section delves into CMakeLists.txt — the most important build configuration file of the C++ package just learned.
Get new articles and lessons by email
When a new technical article or course module is published, Atlas Robotics sends you a short email. No spam.
Unsubscribe any time with the link in each email. See our Privacy policy.