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

Problem getting ROS environment variables with python

asked 2018-09-05 07:54:01 -0600

Lycea gravatar image

I'm trying to get the environment variables ROS_MASTER_URI ,ROS_IP and ROS_HOSTNAME with a python script to show it in some information screen for the user. When I open a terminal and echo myself them I get the expected result. But when I do the same with the script I don't get any values like they were not set at all. I tried these ways already:

  1. print(os.environ.get("ROS_MASTER_URI"))
  2. proc = subprocess.Popen('echo "$ROS_MASTER_URI"',shell=True, universal_newlines=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.PIPE, executable="/bin/bash")
  3. print(os.environ)

Does anyone know why this is not working as expected ? I'm using python version 3.5 and my ros version is kinetic on an ubuntu 16.04

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2018-09-05 08:09:40 -0600

gvdhoorn gravatar image

Are you starting your Python script from a bash session that has sourced the relevant setup.bash file?

If not, then those variables will not be set and that would explain what you report.

edit flag offensive delete link more


Thank you, that actually was the problem. I thought about it bevore but I was the opinion it already used the bashrc for initialisation. After a small tweak with adding source it now worked :)

fixed with source /opt/ros/kinetic/setup.bash;echo " $ROS_MASTER_URI"

Lycea gravatar image Lycea  ( 2018-09-06 02:00:05 -0600 )edit

Question Tools

1 follower


Asked: 2018-09-05 07:54:01 -0600

Seen: 434 times

Last updated: Sep 05 '18