ROS 2 LearningJune 02, 20246 min read

How to Add Custom Libraries to a ROS 2 Python Package

Learn to create custom libraries for ROS 2 Python packages. Enhance your robotics projects with reusable code and improve your development workflow!

How to Add Custom Libraries to a ROS 2 Python Package

Share :

Quick answer

Learn to create custom libraries for ROS 2 Python packages. Enhance your robotics projects with reusable code and improve your development workflow!

Quick Answer

Learn to create custom libraries for ROS 2 Python packages. Enhance your robotics projects with reusable code and improve your development workflow!

Who This Is For

  • ROS 2 Learner
  • Robotics Student
  • Software Developer

What You Will Learn

Introduction

When developing a project, we often fall short on features. Sometimes there are features that we need for example, 2D mapping of the environment, to progress in our project of autonomous robots. Implementing those features from scratch takes a ton of time. In those cases, we have libraries that already have those features, and we only need to understand them. Let's clear up our thoughts about what we understand and what our learning outcomes will be after this article on the ROS 2 Python package external modules. There are some amazing Python libraries that you can integrate into your projects. But before that, let'sclarifyr the concepts of integerating libraries, modules.

Starting Point

No understanding of

  • ROS 2 Python Packages and Modules
  • ROS 2 Package Structure
  • Creating Customized Modules

Learning outcomes

Easily able to

  • Creating Custom Modules into ROS 2 Packages
  • Install External Libraries to your ROS 2 Packages
  • Concept clearning of Python Package, Library and module

Python Libraries Concept and its Terminologies

When I started working on python packages, there were some python jargons that were disturbing me. So lets first clear our concept about these python terminologies

Python Module

It is asingle file containing Python codethat defines functions, classes, and variables. It allows you to organise your code into reusable units. On the right, we have an example of a simple Python module namedmy_module.py: To use this module in another Python file, you would import it like this:


# my_module.py
def greet(name):
 return f"Hello, {name}!"
def add(a, b):
 return a + b
import my_module
print(my_module.greet("Robotisim"))
print(my_module.add(9, 6))

ros2 python nodes

Python Library

It is acollection of modulesthat provide specific functionality. Libraries can be built-in (provided with Python) or external (installed separately). Here's an example of using the built-in math library: Externally built libraries can be used after installation through

pip install <name> # example numpy

import math
import numpy
print(math.sqrt(16)) # Output: 4.0
print(math.pi) # Output: 3.141592653589793

Python Package

It is adirectory that contains one or more modules. It also includes a special file called **__init__.py** to indicate that the directory should be treated as a package. Here's an example of a simple package structure on the right. In this example, module1.py and module2.py are Python modules, and__init__.py can be empty or contain initialization code for the package. To use this package, you would import its modules, as mentioned on the right.

my_package/
|-- __init__.py
|-- module1.py
`-- module2.py
from my_package import module1, module2
print(module1.some_function())
print(module2.some_other_function())

Lets summarize python concepts

  • Python code is typically distributed and executed in its source form(code or script), without an explicit compilation step.
  • In Python, packages are not compiled into a binary format like libraries in some other languages, such as C++.
  • We bundle Python code into a form that is more easily distributable, using tools likedistutils. This is where ros2 python packages comes in.

ROS 2 Python Package Structure

When we create a python package using ROS 2 command, we have couple of things to understand

  • **ros2 pkg create**This is the command to create a new ROS 2 package.
  • **--build-type ament_python** : Specifies that the package will be built using the Ament build system for Python packages.
  • **--node-name** vision_start**** : Specifies the name of the node that will be created in the package.
  • **** vision_pkg****: specifies the name of the package.
ros2 pkg create --build-type ament_python --node-name vision_start vision_pkg

When we execute this command, we see a package with a node automatically created with a fixed structure. This below structure is produced mainly for keeping standards fixed for compatibility between tools and ease of maintenance.

  • For example : the convention of having the package name match thedirectory nameinside the src directory is not a strict requirement, but it is a recommended best practice.
**vision_pkg**/
|-- package.xml
|-- CMakeLists.txt
|-- setup.py
|-- src/
 `--*vision_pkg*/
 |-- __init__.py
 `-- vision_start.py

Creating and Utilising Custom Module

Lets start by understanding example directory tree for a Python package with custom modules for Object detection andObject tracking

**vision_pkg**/
|-- package.xml
|-- setup.py
`-- src/
 `-- **vision_pkg** /
 |-- __init__.py
 |-- vision_start.py
 |--*obstacle_detection*/
 | |-- __init__.py
 | `-- detector.py
 `--*object_tracking*/
 |-- __init__.py
 `-- tracker.py
#!/usr/bin/env python3
import rclpy
from vision_pkg.obstacle_detection.detector import detect_obstacles
from vision_pkg.object_tracking.tracker import track_objects
class VisionNode(Node):
 def __init__(self):
 super().__init__('vision_node')
 self.get_logger().info('Vision node initialized')
 def modules(self, image,objects):
 obstacles = detect_obstacles(image)
 tracks = track_objects(objects)
...

That is not it; you need to mention in the thesetup.py fileany library you are using for the feature you have implemented,, for example:

 install_requires=[
 'setuptools',
 'opencv', # Assuming that we using OpenCV for obstacle detection
 'kalmanfilter', # Assuming that we using Kalman filter for object tracking
 'rclpy',
 ],

Similar questions to answer?

  1. How do I add external Python libraries to a ROS 2 Python package?
  • Answer: You can add external Python libraries to a ROS 2 Python package by including them in the install_requires list in your setup.py file. For example, if you're using numpy for array operations, you can add 'numpy' to the install_requires list.
  1. Can I use custom Python modules in my ROS 2 Python package?
  • Answer: Yes, you can use custom Python modules in your ROS 2 Python package. Simply create your custom modules in a directory within your package, and import them in your Python nodes as needed.
  1. How do I ensure that my ROS 2 Python package includes all necessary dependencies?
  • Answer: To ensure that your ROS 2 Python package includes all necessary dependencies, list them in the install_requires list in your setup.py file. When you build and install your package, these dependencies will be installed automatically.

ROS 2 Python package Libraries Quiz

Practical Example

A practical way to use this article is to connect the concept to a small robot workflow: identify the input, the processing step, and the output you expect from the robot. If the article involves ROS 2, test the idea in a small workspace or simulation before applying it to a larger robot project.

Common Mistakes

  • Trying to memorize the term without connecting it to a robot behavior.
  • Skipping the prerequisite concepts that make the workflow easier to debug.
  • Copying commands or code without checking what each node, topic, file, or parameter is responsible for.
  • Treating one tutorial as a complete roadmap instead of linking it to the next concept.

How This Connects to Other Topics

  • 3D Printing Robotics Hardware for an Autonomous Robot Build
  • How to Collect Raw Sensor Data for Robotics with ROS 2
  • How to Start Developing in ROS 2: A Beginner-Friendly Guide
  • How to Finish Your First ROS Robotics Project
  • How to Start with ROS 2

Learn Next

  • 3D Printing Robotics Hardware for an Autonomous Robot Build
  • How to Collect Raw Sensor Data for Robotics with ROS 2
  • How to Start Developing in ROS 2: A Beginner-Friendly Guide
  • How to Finish Your First ROS Robotics Project
  • How to Start with ROS 2
  • ROS 2 Foundation Path

FAQ

Is How to Add Custom Libraries to a ROS 2 Python Package suitable for beginners?

Yes. The article is written to make the concept easier to understand, while still connecting it to practical robotics work.

What should I learn before this topic?

Start with the prerequisite ideas listed in the article, then connect them to a small project or simulation so the concept becomes concrete.

How does this topic connect to real robots?

It helps you understand how software, sensors, control, simulation, or career decisions show up in practical robot development.

What should I do after reading this article?

Pick one related concept from the Learn Next section and build a small example that uses it.

Can I learn this through Robotisim?

Yes. Robotisim connects these concepts to structured learning paths and project-based robotics practice.

Final Summary

How to Add Custom Libraries to a ROS 2 Python Package is part of the broader ROS 2 Learning learning path. The key is to understand the concept, connect it to a real robot workflow, and then practice it through a focused project instead of learning it in isolation.

Connected learning path

This article supports ROS 2 Foundation Path, especially ROS 2.

Learn with Robotisim

Start learning ROS 2 step by step inside Robotisim.

Explore the academy

Learn next

3D Printing Robotics Hardware for an Autonomous Robot Build
Jun 03, 2025|7 min read

3D Printing Robotics Hardware for an Autonomous Robot Build

Discover how to 3D print robotics hardware for building autonomous robots. Learn key steps from assembly to motion control with ROS 2

Read more
How to Collect Raw Sensor Data for Robotics with ROS 2
Jun 05, 2025|7 min read

How to Collect Raw Sensor Data for Robotics with ROS 2

Learn to integrate sensors with ROS 2 and collect raw sensor data for robotics. Guide on real time data processing and sensor fusion for autonomous robots.

Read more
How to Start Developing in ROS 2: A Beginner-Friendly Guide
Jun 02, 2024|8 min read

How to Start Developing in ROS 2: A Beginner-Friendly Guide

Begin your ROS 2 development journey with our guide. Learn key concepts and setup for creating effective robotics applications in no time!

Read more
How to Finish Your First ROS Robotics Project
Jul 24, 2025|8 min read

How to Finish Your First ROS Robotics Project

Struggling to finish your first ROS 2 project? This step by step guide helps you go from hardware setup to obstacle avoidance using micro ROS and ESP32.

Read more