# Problem getting ROS environment variables with python

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 close merge delete ## 1 Answer Sort by » oldest newest most voted 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. more ## Comments 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"

( 2018-09-06 02:00:05 -0500 )edit