Ask Your Question

Programmatically create a node with a custom namespace in ROS 1

asked 2020-05-29 05:46:26 -0500

anonymous user


updated 2020-05-29 09:44:22 -0500

sloretz gravatar image


I wanted to know if there is a way to create programmatically in Python a ROS Node with a certain namespace decided by me. I know that in ROS 2 this is possible but I can't seem to find anything similar to it on ROS. I'm using ROS Noetic and Python 3.

Thank you!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2020-05-29 09:43:23 -0500

sloretz gravatar image

updated 2020-05-29 13:44:57 -0500

It looks like the namespace can be set programmatically in ROS 1 by setting ROS_NAMESPACE prior to importing rospy. With the code below, the node will have the namespace /my/custom/namespace unless the user running the node sets ROS_NAMESPACE or passes the command line argument __ns:=/some/other/ns.

import os
import sys

if 'ROS_NAMESPACE' not in os.environ:
    # Default namespace if not set
    # Note, didn't need to check sys.argv for `__ns:=...`,
    # it seems __ns:= takes precidence over ROS_NAMESPACE

    os.environ['ROS_NAMESPACE'] = '/my/custom/namespace'

# Must set `os.environ['ROS_NAMESPACE']` BEFORE importing `rospy`
import rospy

from std_msgs.msg import String

rospy.Subscriber("chatter", String, lambda m: rospy.loginfo(

I tested this using ROS Melodic, but I would expect it to work in ROS Noetic as well.

EDIT @gvdhoorn has a good point. While the above answers the literal question, the preferred approach is to set the namespace when starting the nodes. The code of a node should only focus on its purpose; it shouldn't have to be concerned about whether there is one or many robots in different namespaces using that node. The awareness of how the node is integrated into a larger system should only be with the tools that launch the node.

You already discovered it can be set using the ns attribute in launch files. The namespace can be set one node at a time, or on a whole group of nodes at once. I took this example from the ROS wiki and simplified it for this answer.

  <!-- Start a single node in the namespace foo -->
  <node ns="foo" name="listener1" pkg="rospy_tutorials" type="listener"/>
  <!-- Start a group of nodes in the namespace bar-->
  <group ns="bar">
    <node pkg="rospy_tutorials" type="listener" name="listener"/>
    <node pkg="rospy_tutorials" type="talker" name="talker"/>
edit flag offensive delete link more


It would perhaps also be good to ask why the OP wants to do this, as placing nodes in namespaces (and remapping topics, setting parameters, etc) are typically considered either configuration or deployment time activities.

gvdhoorn gravatar image gvdhoorn  ( 2020-05-29 09:45:06 -0500 )edit

Thank you! This works yes! Another way I found was to put the namespace on the launch file using the field 'ns'. I want to do this because I want to have a multi-robot system where each robot has the same number of servers and each one accesses their corresponding servers.

anonymous userAnonymous ( 2020-05-29 12:56:36 -0500 )edit

Ah got it. Updated answer to describe how to change the namespace using launch files.

sloretz gravatar image sloretz  ( 2020-05-29 13:45:26 -0500 )edit

@PedroACaldeira: please use the approach shown by @sloretz. Do not put nodes in namespaces by changing os.environ manually inside the nodes themselves.

gvdhoorn gravatar image gvdhoorn  ( 2020-05-29 13:51:34 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2020-05-29 05:46:26 -0500

Seen: 145 times

Last updated: May 29 '20