0
$\begingroup$

Rosanswers logo

Hello! I need some help as I was trying to write a python script which could run some ROS commands, such as $rostopic list and $rostopic echo(while some nodes are running). I tried something like the following, but none of them works. when I tried this:

os.system("rostopic list")

I got this error when I ran the above: sh: 1: rostopic: not found

Or this:

subprocess.call(["rostopic", "list"])

It was not working and a error message was given like this "FileNotFoundError: [Errno 2] No such file or directory: 'rostopic'"

I was thinking about execute a shell script from my python script, but I don't know how to write one for running ROS commands.

I appreciate any help!


Originally posted by Radar on ROS Answers with karma: 1 on 2017-05-29

Post score: 0

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

You can get the topics much easier using the rospy client functions rospy client module

import rospy
l = rospy.get_published_topics()

Originally posted by NEngelhard with karma: 3519 on 2017-05-29

This answer was ACCEPTED on the original site

Post score: 1

$\endgroup$
0
$\begingroup$

Rosanswers logo

I would do what @NEngelhard suggests, but just to answer your question: you need to run your Python script in an environment that has been setup with the ROS environment variables.

Two ways to do that:

  • start your script from a terminal in which you've sourced the right setup.bash
  • setup the environment which is created by os.system(..) with the needed environment variables

If you do want to continue starting ROS command line utilities, I'd recommend you use subprocess or popen instead of os.system(..).


Originally posted by gvdhoorn with karma: 86574 on 2017-05-29

This answer was NOT ACCEPTED on the original site

Post score: 1

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.